7

I need to create Jenkins agent cloud which runs under Windows VMs on Amazon EC2.

My view of this is simple scenario:

I have few of pre-configures AMIs, each of VM have specific environment which matches one of my projects. I have few projects to build often enough to keep VM running. But some builds will run weekly, others mounthly... Jenkins should be able to start VM automatically when project should be built and terminate VM when build is completed. I have several BCB projects and many .NET projects, Windows as slave VM OS is absolutely necessary.

It is not a problem to prepare pre-configured AMI where Jenkins slave is installed and configured. But I have no idea how to manage such slave VMs from master (run/terminate them)

I found Amazon EC2 plugin which can be used to run and terminate VMs. But it also tries to install and run slave there. Unfortunately, windows slaves are not supported yet. Is there a way to use pre-configured AMIs or let Amazon EC2 plugin install agent on Windows VM?

I tried to use TeamCity also - it can run pre-configured windows AMI and build projects there (exact my scenario). But I need too many VMs and my boss is not ready to pay for licenses (3 free licenses are not enough)

Is it possible to use Jenkins for my scenario? Is it any other alternatives?

1 Answers1

0

boto.ec2 can be perfectly used to start / stop / terminate instances on the go.

I use a script for that. Here is a piece of it that I can share. I'm not able to share some parts. Thank you for understanding.

#!/usr/bin/python
import boto.ec2
import sys
import time

# specify AWS keys
auth = {"aws_access_key_id": "YOUR_KEY", "aws_secret_access_key": "YOUR_SECRET_KEY"}

def main():
    # read arguments from the command line and
    # check whether at least two elements were entered
    if len(sys.argv) < 2:
        print "Usage: python aws.py {start|stop}\n"
        sys.exit(0)
    else:
        action = sys.argv[1]

    if action == "start":
        startInstance()
    elif action == "stop":
        stopInstance()
    else:
        print "Usage: python aws.py {start|stop}\n"

def startInstance():
    print "Starting the instance..."

    # change "eu-west-1" region if different
    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    # change instance ID appropriately
    try:
        instances = ec2.start_instances(instance_ids="ID_INSTANCE TO START")

        instances[0].update()
        while instances[0].state != "running":
            print instances[0].state
            time.sleep(5)
            instances[0].update()

#this part manage the association of Elastic IP
        ec2.associate_address("ID_INSTANCE","ELASTIC IP")


    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

def stopInstance():
    print "Stopping the instance..."

    try:
        ec2 = boto.ec2.connect_to_region("eu-west-1", **auth)

    except Exception, e1:
        error1 = "Error1: %s" % str(e1)
        print(error1)
        sys.exit(0)

    try:
        ec2.stop_instances(instance_ids="INSTANCE_ID")

        instances[0].update()
        while instances[0].state != "stopped":
            print instances[0], instances[0].state
            time.sleep(5)
            instance.update()

        print "Instance stopped : "

    except Exception, e2:
        error2 = "Error2: %s" % str(e2)
        print(error2)
        sys.exit(0)

if __name__ == '__main__':
    main()
aorfevre
  • 5,034
  • 3
  • 21
  • 51