示例#1
0
def build(box, base, debug):
    """Builds vagrant box"""

    organization = box[:box.rfind('/')]
    box_name = box[box.rfind('/') + 1:box.rfind(':')]
    version_ = box[box.rfind(':') + 1:]

    template = Template("""
    {
      "builders": [
        {
          "box_name"    : "{{ box }}",
          "output_dir"  : "/tmp/vagrant/build/{{ organization }}/{{ box_name }}",
          "box_version" : "{{ version }}",      
          "communicator": "ssh",
          "source_path" : "{{ base }}",
          "provider"    : "virtualbox",
          "skip_add"    : true,
          "type"        : "vagrant"
        }
      ],
      "provisioners": [
        {
          "ansible_env_vars": [ "ANSIBLE_STDOUT_CALLBACK=debug" ],
          "extra_arguments" : [ "--extra-vars", "target=default user=vagrant ansible_os_family=Debian" ],
          "type"            : "ansible",
          "playbook_file"   : "{{ box_name }}.yml",
          "user"            : "vagrant"
        }
      ]       
    }""")

    try:
        os.makedirs(f'/tmp/vagrant/template/{organization}/{box_name}')
    except OSError:
        # do nothing
        pass

    output = template.render(box=box,
                             base=base,
                             box_name=box_name,
                             organization=organization,
                             version=version_)
    template_path = f'/tmp/vagrant/template/{organization}/{box_name}/{box_name}.json'
    f = open(template_path, 'w+')
    f.write(output)
    f.close()
    if debug:
        print(output)

    script_path = exec.get_script_path(
        f'build/box.sh clean {organization} {box_name}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)

    script_path = exec.get_script_path(f'build/box.sh build {template_path}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)
示例#2
0
def push(box, debug, skip):
    """Publishes vagrant box to target environment"""

    organization = box[:box.rfind('/')]
    box_name = box[box.rfind('/') + 1:box.rfind(':')]
    version_ = box[box.rfind(':') + 1:]

    if not skip:
        print("scp-ing the box")
        script_path = exec.get_script_path(
            f'build/box.sh push {organization} {box_name}')
        returncode, lines = exec.run(script_path, False)
        if returncode != 0:
            sys.exit(1)

    try:
        os.makedirs(f'/tmp/vagrant/metadata/{organization}/{box_name}')
    except OSError:
        # do nothing
        pass

    metadata_template = Template("""
    {
      "description": "",
       "name": "{{ organization }}/{{ box_name }}",
       "versions": [
         {
           "providers": [
             {
              "checksum": "{{ sha1sum }}",
              "checksum_type": "sha1",
              "name": "virtualbox",
              "url": "http://tmt.7onetella.net/boxes/{{ organization }}/{{ box_name }}/package.box"
             }
           ],
           "version": "{{ version }}"
         }
       ]
    }""")
    metadata_output = metadata_template.render(
        box=box,
        organization=organization,
        box_name=box_name,
        sha1sum=hash_util.sha1sum(
            f'/tmp/vagrant/build/{organization}/{box_name}/package.box'),
        version=version_)
    metadata_json_path = f'/tmp/vagrant/metadata/{organization}/{box_name}/metadata.json'
    f = open(metadata_json_path, 'w+')
    f.write(metadata_output)
    f.close()
    if debug:
        print(metadata_output)

    print("scp-ing the metadata.json")
    script_path = exec.get_script_path(
        f'build/box.sh metadata {organization} {box_name}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)
示例#3
0
def ssh(box, debug):
    """SSH to vagrant test Vagrant instance"""

    organization = box[:box.rfind('/')]
    box_name = box[box.rfind('/') + 1:]

    script_path = exec.get_script_path(
        f'build/box.sh ssh {organization} {box_name}')
    exec.fork(script_path, debug)
示例#4
0
文件: remote.py 项目: 7onetella/vag
def build(repo: str, branch: str, debug: bool):
    """builds your project"""
    script_path = exec.get_script_path(f'go.sh build {repo} {branch}')

    if debug:
        print(f'script_path={script_path}')
    return_code = exec.run_raw(script_path)
    if return_code != 0:
        sys.exit(1)
示例#5
0
def clean(box, debug):
    """Cleans up vagrant build, terminates vagrant instance etc"""

    organization = box[:box.rfind('/')]
    box_name = box[box.rfind('/') + 1:]

    script_path = exec.get_script_path(
        f'build/box.sh clean {organization} {box_name}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)
示例#6
0
def test(box, debug):
    """Start a test Vagrant instance"""

    organization = box[:box.rfind('/')]
    box_name = box[box.rfind('/') + 1:]

    script_path = exec.get_script_path(
        f'build/box.sh test {organization} {box_name}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)
示例#7
0
def list(debug):
    """lists running vagrant instances"""

    script_path = exec.get_script_path('instance/list.sh')

    returncode, lines = exec.run(script_path, True)
    if returncode != 0:
        sys.exit(1)

    home = expanduser("~")

    for instance in lines:
        if len(instance) > 0:
            f = open(f'{home}/VirtualBox VMs/{instance}/{instance}.vbox', 'r')
            xml_str = f.read()

            obj = untangle.parse(xml_str)
            machine = obj.VirtualBox.Machine
            os_type = machine['OSType']
            memory = machine.Hardware.Memory['RAMSize']
            name = machine['name']
            shared_folder = machine.Hardware.SharedFolders.SharedFolder[
                'hostPath']
            vagrant_f = open(shared_folder + '/Vagrantfile', 'r')
            ip = ''
            for line in vagrant_f.readlines():
                if 'config.vm.network' in line and 'ip:' in line:
                    tokens = line.split(',')
                    for token in tokens:
                        if 'ip:' in token:
                            ip = token.replace('ip:',
                                               '').strip().replace('"', '')

            print(f"""
name   : {name}
shared : {shared_folder}
os     : {os_type}
ip     : {ip}
memory : {memory}""")
            f.close()
示例#8
0
文件: remote.py 项目: 7onetella/vag
def deploy(repo: str, stage: str, debug: bool):
    """deploys your project"""
    script_path = exec.get_script_path(f'go.sh deploy {repo} {stage}')
    return_code = exec.run_raw(script_path)
    if return_code != 0:
        sys.exit(1)
示例#9
0
def deploy(repo_name_revision, debug):
    """deploys docker image in nomad environment"""

    # docker-registry.7onetella.net/7onetella/password-dev:0.8.4
    last_slash_idx = repo_name_revision.rfind('/')
    docker_registry_uri = repo_name_revision[:last_slash_idx]
    name_revision = repo_name_revision[last_slash_idx + 1:]

    tokens = name_revision.split(':')
    service, group = get_service_and_group(tokens[0])
    if debug:
        print(f'service = {service}, group = {group}')
    version = tokens[1]

    image = f'{docker_registry_uri}/{service}:{version}'
    if debug:
        print(f'image = {image}')

    template = Template("""
    job "{{ service }}" {
      datacenters = ["dc1"]

      type = "service"

      update {
        stagger      = "60s"
        max_parallel = 1
      }

      group "{{ group }}" {
        count = 1
        network {
            port "http" { to = {{ port }} }
            port "ssh"  { to = 22 }
        }            
            
        task "container" {
            driver = "docker"
            config {
                image = "{{ image }}"
                ports = [ "http", "ssh" ]{% if log_driver is not none %}
                
                logging {
                   type = "elasticsearch"
                   config {
                        elasticsearch-url="https://elasticsearch-dev.7onetella.net:443"
                        elasticsearch-sniff=false
                        elasticsearch-index="docker-%F"
                        elasticsearch-type="_doc"
                        elasticsearch-timeout="60s"
                        elasticsearch-version=5
                        elasticsearch-fields="containerID,containerName,containerImageName"
                        elasticsearch-bulk-workers=1
                        elasticsearch-bulk-actions=1000
                        elasticsearch-bulk-size=1024
                        elasticsearch-bulk-flush-interval="1s"                   
                    }
                }{% endif %}

                volumes = [
                    "/var/run/docker.sock:/var/run/docker.sock"
                ]                
            }
    
            resources {
                cpu = 20
                memory = {{ memory }}
            }{% if health_check is not none %}

            service {
                tags = [ {% for tag in tags %}{% if loop.index0 > 0 %},{% endif %} "{{tag}}"{% endfor %} ]
                port = "http"
                check {
                    type     = "http"
                    path     = "{{ health_check }}"
                    interval = "10s"
                    timeout  = "2s"
                }
            }{% else %}

            service {
                port = "ssh"
            }{% endif %}
    
            env {  {% for key, value in envs.items() %}
                {{ key }} = "{{ value }}"{% endfor %}                
            }
        }
      }
    }""")

    current_dir = os.getcwd()
    app_file = f'{current_dir}/{service}-{group}.app'
    if debug:
        print(f'app_file = {app_file}')

    try:
        data = config.read(app_file)
    except IndexError:
        print(f'error while processing {app_file}')
        sys.exit(1)

    if debug:
        print(f'data is \n {data}')

    # if image is specified use it stead of deriving it from service name
    image_from_config = get(data, 'image', '')
    if image_from_config:
        image = image_from_config

    tags = data['tags']

    if not tags:
        urlprefix = f'urlprefix-{ service }-{ group }.7onetella.net/'

        host = get(data, 'host', '')
        path = get(data, 'path', '/')
        if host:
            urlprefix = f'urlprefix-{host}{path}'

        tags.append(urlprefix)
    if debug:
        print(f'tags = {tags}')

    try:
        os.makedirs(f'/tmp/nomad')
    except OSError:
        # do nothing
        pass

    output = template.render(service=service,
                             group=group,
                             image=image,
                             memory=get(data, 'memory', 128),
                             port=get(data, 'port', 4242),
                             health_check=get(data, 'health', None),
                             log_driver=get(data, 'log_driver', None),
                             tags=tags,
                             envs=data['envs'])
    template_path = f'/tmp/nomad/{service}-{group}.nomad'
    f = open(template_path, 'w+')
    f.write(output)
    f.close()
    if debug:
        print(output)
        return 0

    script_path = exec.get_script_path(f'nomad.sh {template_path}')
    returncode, lines = exec.run(script_path, False)
    if returncode != 0:
        sys.exit(1)