예제 #1
0
def init_security(server_container, url, hop_config):
    # copy passwd
    console("Updating passwd")
    copy_to_container(src=hop_config.passwd_path,
                      dest='/etc/go/passwd',
                      container=server_container,
                      owner='go',
                      group='go')

    # add security to cruise-config.xml
    response = requests.get(
        '{}/go/admin/restful/configuration/file/GET/xml'.format(url))
    xml, md5 = response.content, response.headers.get('x-cruise-config-md5',
                                                      None)
    if not md5:
        return

    console("Adding security config to cruise-config.xml")
    security = '<security><passwordFile path="/etc/go/passwd"/><admins><user>admin</user></admins></security>'
    cruise_config = fromstring(xml)
    cruise_config.find('server').insert(0, fromstring(security))
    requests.post(
        '{}/go/admin/restful/configuration/file/POST/xml'.format(url),
        data={
            'xmlFile': tostring(cruise_config),
            'md5': md5
        })
예제 #2
0
def provision(hop_config):
    console("Using local_docker provider")

    client = docker.from_env()
    ensure_images_available(client, hop_config)
    network_name = hop_config.get('provider.network', 'hopnetwork')
    server_config = _server_config(hop_config, network_name)
    url = 'http://localhost:{}'.format(server_config['ports'][8153])

    # create network
    maybe_network = [
        n for n in client.networks.list() if n.name == network_name
    ]
    if len(maybe_network) == 0:
        network = client.networks.create(network_name, driver="bridge")
    else:
        network = maybe_network[0]

    # run go server
    server_container = run_go_server(client, server_config, network,
                                     hop_config)

    wait_for_go_server(url)
    init_security(server_container, url, hop_config)

    run_go_agent(client, hop_config, network, network_name, server_config)

    wait_for_go_server(url)
    console("GoCD is up and running on {}".format(url))
예제 #3
0
def ensure_images_available(client, hop_config):
    console("Verifying presense of images")
    for image in [hop_config.agent_image, hop_config.server_image]:
        try:
            client.images.get(image)
        except docker.errors.ImageNotFound:
            console("Image {} not found. Attempting to pull.".format(image))
            client.images.pull(image)
예제 #4
0
def ensure_images_available(client, hop_config):
    console("Verifying presense of images")
    agent_image = hop_config.get('provider.agents.image', 'gocd/gocd-agent')
    server_image = hop_config.get('provider.server.image', 'gocd/gocd-server')
    for image in [agent_image, server_image]:
        try:
            client.images.get(image)
        except docker.errors.ImageNotFound:
            console("Image {} not found. Attempting to pull.".format(image))
            client.images.pull(image)
예제 #5
0
def _run_go_server(client, hop_config, network):
    go_server_image = hop_config.server_image
    maybe_server_containers = [c for c in client.containers.list() if c.name == hop_config.server_name]

    if len(maybe_server_containers) == 0:
        console("Starting GoCD server from {0}".format(go_server_image))
        log.debug('creating SERVER with config %s', hop_config.server_config)
        server = client.containers.run(go_server_image, **hop_config.server_config)
        network.connect(server)
        return server
    else:
        return maybe_server_containers[0]
예제 #6
0
def destroy(args, hop_config):  # pylint: disable=unused-argument
    console("Destroying GoCD")
    client = docker.from_env()
    hop_containers = [
        c for c in client.containers.list(all=True)
        if _is_hop_container(c, hop_config)
    ]

    for container in hop_containers:
        try:
            container.kill()
            container.remove()
        except Exception as exception:
            print(exception)
예제 #7
0
    def execute(self):
        try:
            provisioner = get_provisioner(self.hop_config['provider']['name'])
        except KeyError as exception:
            print("Error initializing provider. Make sure your configuration is correct")
            print(exception)
            exit(1)
        except ImportError as exception:
            print("Error initializing provider. Make sure your configuration is correct")
            print(exception)
            exit(1)

        console("Provisioning GoCD")
        provisioner.provision(self.hop_config)
예제 #8
0
def provision(hop_config):
    console("Using local_docker provider")
    client = docker.from_env()
    hop_config = LocalDockerConfig(hop_config)

    ensure_images_available(client, hop_config)
    network = _create_network(client, hop_config.network_name)
    server_container = _run_go_server(client, hop_config, network)
    _wait_for_go_server(hop_config.https_url)
    _init_security(server_container, hop_config)
    _run_go_agent(client, hop_config, network)
    _wait_for_go_server(hop_config.https_url)

    console("GoCD is up and running on {}".format(hop_config.https_url))
예제 #9
0
def _wait_for_go_server(url):
    tries = 0
    while tries < 5:
        try:
            requests.get('{}/go/auth/login'.format(url), verify=False)
            console("GoCD is up and running".format(url))
            return
        except Exception as exception:
            log.info("while waiting for gocd to be up, got %s", exception)
            tries += 1
            console("GoCD not yet initialized at {}. Will try again in 15 secs".format(url))
            time.sleep(15)
    print("ERROR: there was a problem initializing gocd. Check the server logs")
    exit(1)
예제 #10
0
def run_go_server(client, server_config, network, hop_config):
    go_server_image = hop_config.get('provider.server.image',
                                     'gocd/gocd-server')
    maybe_server_containers = [
        c for c in client.containers.list() if c.name == server_config['name']
    ]

    if len(maybe_server_containers) == 0:
        console("Starting GoCD server from {0}".format(go_server_image))
        log.debug('creating SERVER with config %s', server_config)
        server = client.containers.run(go_server_image, **server_config)
        network.connect(server)
        return server
    else:
        return maybe_server_containers[0]
예제 #11
0
def run_go_agent(client, hop_config, network, network_name, server_config):
    number_of_agents = hop_config.get('provider.agents.instances', 1)
    server_hostname = server_config['hostname']
    go_agent_image = hop_config.get('provider.agents.image', 'gocd/gocd-agent')
    go_agent_name_prefix = hop_config.get('provider.agents.prefix',
                                          'hop-agent')

    maybe_agents_containers = [
        c for c in client.containers.list()
        if c.name.startswith(go_agent_name_prefix)
    ]
    for i in range(0, number_of_agents):
        agent_name = "{0}-{1}".format(go_agent_name_prefix, i)
        if agent_name not in [a.name for a in maybe_agents_containers]:
            console("Starting {0} from {1}".format(agent_name, go_agent_image))
            agent_config = _agent_config(server_hostname, agent_name,
                                         network_name)
            log.debug('creating AGENT with config %s', agent_config)
            agent = client.containers.run(go_agent_image, **agent_config)
            network.connect(agent)
예제 #12
0
def _run_go_agent(client, hop_config, network):
    new_instance_count = hop_config.agent_instance_count

    # scale down
    agent_containers = sorted([c for c in client.containers.list(all=True) if
                               c.name.startswith(hop_config.agents_prefix)], key=lambda c: c.name)

    for agent in agent_containers[new_instance_count:]:
        console("Stopping and destroying {}".format(agent.name))
        agent.kill()
        agent.remove()

    # scale up
    container_names = sorted([a.name for a in client.containers.list(all=True)])
    for i in range(0, new_instance_count):
        agent_name = "{0}-{1}".format(hop_config.agents_prefix, i)

        if not agent_name in container_names:
            console("Starting {0} from {1}".format(agent_name, hop_config.agent_image))
            log.debug('creating AGENT with config %s', hop_config.agent_config(agent_name))
            agent = client.containers.run(hop_config.agent_image, **hop_config.agent_config(agent_name))
            network.connect(agent)
예제 #13
0
def execute(configurator, app_name, app_config):
    console("executing 'pac' plan for {0}".format(app_name))

    ensure_config_repo(configurator, git_url=app_config['git_url'], plugin='yaml.config.plugin')
예제 #14
0
def execute(args, **kwargs):  # pylint: disable=unused-argument
    console("Provisioning GoCD")

    hop_config = kwargs['hop_config']
    get_provider(hop_config).provision(hop_config)