Ejemplo n.º 1
0
def create_vm(vm_name_prefix, vm_image):
    # _ is not valid in a vm name
    vm_name = '{}-{}'.format(vm_name_prefix.replace('_', '-'), RESOURCE_GROUP)
    az(['vm', 'create', '-g', RESOURCE_GROUP, '-n', vm_name, '--authentication-type', 'ssh', '--image', vm_image, '--admin-username', VM_USERNAME])
    io = StringIO()
    az(['vm', 'list-ip-addresses', '--resource-group', RESOURCE_GROUP, '--name', vm_name, '--query', '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'], _out=io)
    vm_ip_address = io.getvalue().strip().replace('"', '')
    io.close()
    vm = ssh.bake(['-oStrictHostKeyChecking=no', '-tt', "{}@{}".format(VM_USERNAME, vm_ip_address)])
    return vm
Ejemplo n.º 2
0
    def get_instance_ips(self) -> Dict[str, int]:
        from sh import az
        ips = {}
        ipscmd = az('vm', 'list-ip-addresses', '--output', 'yaml')

        thislist = [y for y in (x.strip() for x in ipscmd.splitlines()) if y]
        for i in thislist:
            if 'ipAddress' in i:
                arra = i.split()
                ips[arra[1].rstrip()] = 1

        return ips
Ejemplo n.º 3
0
def create_vm(vm_name_prefix, vm_image):
    # _ is not valid in a vm name
    vm_name = '{}-{}'.format(vm_name_prefix.replace('_', '-'), RESOURCE_GROUP)
    vm_name = hashlib.md5(vm_name.encode()).hexdigest()[:10]
    az([
        'vm', 'create', '-g', RESOURCE_GROUP, '-n', vm_name,
        '--authentication-type', 'ssh', '--image', vm_image,
        '--admin-username', VM_USERNAME, '--deployment-name', vm_name
    ])
    io = StringIO()
    az([
        'vm', 'list-ip-addresses', '--resource-group', RESOURCE_GROUP,
        '--name', vm_name, '--query',
        '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'
    ],
       _out=io)
    vm_ip_address = io.getvalue().strip().replace('"', '')
    io.close()
    vm = ssh.bake([
        '-oStrictHostKeyChecking=no', '-tt',
        "{}@{}".format(VM_USERNAME, vm_ip_address)
    ])
    return vm
Ejemplo n.º 4
0
def main():
    print_env_vars()
    time_str = datetime.utcnow().strftime('%Y%m%d%H%M%S')
    az(["login"], _out=sys.stdout, _err=sys.stdout)
    resource_group = 'azurecli-release-debian-' + time_str
    vm_name = 'vm-debian-' + time_str
    print_status('Creating resource group.')
    az(['group', 'create', '-l', 'westus', '-n', resource_group], _out=sys.stdout, _err=sys.stdout)
    print_status('Creating VM.')
    az(['vm', 'create', '-g', resource_group, '-n', vm_name, '--generate-ssh-keys', '--authentication-type', 'ssh',
        '--image', 'Canonical:UbuntuServer:14.04.4-LTS:latest', '--admin-username', 'ubuntu'],
       _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    print_status('Getting VM IP address.')
    az(['vm', 'list-ip-addresses', '--resource-group', resource_group, '--name', vm_name,
        '--query', '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'], _out=io)
    ip_address = io.getvalue().strip().replace('"', '')
    print_status('VM IP address is {}'.format(ip_address))
    io.close()
    vm_connect_str = "ubuntu@{}".format(ip_address)
    my_vm = ssh.bake(['-oStrictHostKeyChecking=no', vm_connect_str])
    print_status('Installing git.')
    my_vm(['sudo', 'apt-get', 'update', '&&', 'sudo', 'apt-get', 'install', '-y', 'git'],
          _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    repo_dir = io.getvalue().strip()
    io.close()
    print_status('Cloning repo.')
    my_vm(['git', 'clone', 'https://github.com/{}'.format(script_env.get('REPO_NAME')), repo_dir], _out=sys.stdout, _err=sys.stdout)
    path_to_debian_build_script = os.path.join(repo_dir, 'packaged_releases', 'debian', 'debian_build.sh')
    path_to_dir_creator = os.path.join(repo_dir, 'packaged_releases', 'debian', 'debian_dir_creator.sh')
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    build_artifact_dir = io.getvalue().strip()
    io.close()
    print_status('Running debian build scripts.')
    my_vm(['chmod', '+x', path_to_debian_build_script, path_to_dir_creator], _out=sys.stdout, _err=sys.stdout)
    my_vm(['export', 'CLI_VERSION={}'.format(script_env.get('CLI_VERSION')), '&&',
           'export', 'CLI_DOWNLOAD_SHA256={}'.format(script_env.get('CLI_DOWNLOAD_SHA256')), '&&',
           'export', 'BUILD_ARTIFACT_DIR={}'.format(build_artifact_dir), '&&',
           path_to_debian_build_script, path_to_dir_creator],
          _out=sys.stdout, _err=sys.stdout)
    print_status('Debian build complete.')
    io = StringIO()
    my_vm(['ls', build_artifact_dir], _out=io)
    artifact_name = io.getvalue().strip()
    io.close()
    deb_file_path = os.path.join(build_artifact_dir, artifact_name)
    print_status('Installing the .deb on the build machine')
    my_vm(['sudo', 'dpkg', '-i', deb_file_path], _out=sys.stdout, _err=sys.stdout)
    # Upload to Azure Storage
    print_status('Uploading .deb to Azure storage.')
    my_vm(['az', 'storage', 'container', 'create', '--name', 'repos', '--public-access', 'blob',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    my_vm(['az', 'storage', 'blob', 'upload', '-f', deb_file_path,
           '-n', artifact_name, '-c', 'repos', '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    my_vm(['az', 'storage', 'blob', 'url', '-n', artifact_name, '-c', 'repos', '--output', 'tsv',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))], _out=io)
    deb_url = io.getvalue().strip()
    io.close()
    print_status('Debian file uploaded to the following URL.')
    print_status(deb_url)
    # Publish to apt service
    my_vm(['wget', '-q', 'https://bootstrap.pypa.io/get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'python', 'get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'pip', 'install', '--upgrade', 'requests'], _out=sys.stdout, _err=sys.stdout)
    upload_script = REPO_UPLOAD_SCRIPT_TMPL.format(cli_version=script_env.get('CLI_VERSION'),
                                                   repo_id=script_env.get('DEBIAN_REPO_ID'),
                                                   source_url=deb_url,
                                                   repo_package_url=script_env.get('DEBIAN_REPO_URL'),
                                                   repo_user=script_env.get('DEBIAN_REPO_USERNAME'),
                                                   repo_pass=script_env.get('DEBIAN_REPO_PASSWORD'))
    my_vm(['echo', '-e', '"{}"'.format(upload_script), '>>', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['python', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status('Done. :)')
    give_chance_to_cancel('Delete resource group (in background)')
    az(['group', 'delete', '--name', resource_group, '--yes', '--no-wait'], _out=sys.stdout, _err=sys.stdout)
    print_status('Finished. :)')
Ejemplo n.º 5
0
def main():
    print_env_vars()
    time_str = datetime.utcnow().strftime('%Y%m%d%H%M%S')
    az(["login"], _out=sys.stdout, _err=sys.stdout)
    resource_group = 'azurecli-release-docker-' + time_str
    vm_name = 'vm-docker-' + time_str
    print_status('Creating resource group.')
    az(['group', 'create', '-l', 'westus', '-n', resource_group],
       _out=sys.stdout,
       _err=sys.stdout)
    print_status('Creating VM.')
    az([
        'vm', 'create', '-g', resource_group, '-n', vm_name,
        '--generate-ssh-keys', '--authentication-type', 'ssh', '--image',
        'Canonical:UbuntuServer:16.04-LTS:latest', '--admin-username', 'ubuntu'
    ],
       _out=sys.stdout,
       _err=sys.stdout)
    io = StringIO()
    print_status('Getting VM IP address.')
    az([
        'vm', 'list-ip-addresses', '--resource-group', resource_group,
        '--name', vm_name, '--query',
        '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'
    ],
       _out=io)
    ip_address = io.getvalue().strip().replace('"', '')
    print_status('VM IP address is {}'.format(ip_address))
    io.close()
    vm_connect_str = "ubuntu@{}".format(ip_address)
    my_vm = ssh.bake(['-oStrictHostKeyChecking=no', vm_connect_str])
    print_status('Installing Docker.')
    my_vm([
        'curl', '-sSL', 'https://get.docker.com/', '-o',
        'docker_install_script.sh'
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    my_vm(['sh', 'docker_install_script.sh'], _out=sys.stdout, _err=sys.stdout)
    print_status('Docker installed.')
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    repo_dir = io.getvalue().strip()
    io.close()
    print_status('Cloning repo.')
    my_vm([
        'git', 'clone', 'https://github.com/{}'.format(
            script_env.get('REPO_NAME')), repo_dir
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    image_tag = '{}:{}'.format(script_env.get('DOCKER_REPO'),
                               script_env.get('CLI_VERSION'))
    path_to_dockerfile = os.path.join(repo_dir, 'packaged_releases', 'docker',
                                      'Dockerfile')
    path_to_docker_context = os.path.join(repo_dir, 'packaged_releases',
                                          'docker')
    print_status('Running Docker build.')
    my_vm([
        'sudo', 'docker', 'build', '--no-cache', '--build-arg',
        'BUILD_DATE="`date -u +"%Y-%m-%dT%H:%M:%SZ"`"', '--build-arg',
        'CLI_VERSION={}'.format(script_env.get('CLI_VERSION')), '--build-arg',
        'CLI_DOWNLOAD_SHA256={}'.format(script_env.get('CLI_DOWNLOAD_SHA256')),
        '-f', path_to_dockerfile, '-t', image_tag, path_to_docker_context
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    print_status('Docker build complete.')
    print_status('Running Docker log in.')
    my_vm([
        'sudo', 'docker', 'login', '--username',
        script_env.get('DOCKER_USERNAME'), '--password', '"{}"'.format(
            script_env.get('DOCKER_PASSWORD'))
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    print_status('Running Docker push.')
    my_vm(['sudo', 'docker', 'push', image_tag],
          _out=sys.stdout,
          _err=sys.stdout)
    print_status('Image pushed to Docker Hub.')
    print_status('Done. :)')
    give_chance_to_cancel('Delete resource group (in background)')
    az(['group', 'delete', '--name', resource_group, '--yes', '--no-wait'],
       _out=sys.stdout,
       _err=sys.stdout)
    print_status('Finished. :)')
Ejemplo n.º 6
0
def main():
    print_env_vars()
    time_str = datetime.utcnow().strftime('%Y%m%d%H%M%S')
    az(["login"], _out=sys.stdout, _err=sys.stdout)
    resource_group = 'azurecli-release-rpm-' + time_str
    vm_name = 'vm-rpm-' + time_str
    print_status('Creating resource group.')
    az(['group', 'create', '-l', 'westus', '-n', resource_group], _out=sys.stdout, _err=sys.stdout)
    print_status('Creating VM.')
    az(['vm', 'create', '-g', resource_group, '-n', vm_name, '--generate-ssh-keys', '--authentication-type', 'ssh',
        '--image', 'OpenLogic:CentOS:7.3:latest', '--admin-username', 'myuser'],
       _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    print_status('Getting VM IP address.')
    az(['vm', 'list-ip-addresses', '--resource-group', resource_group, '--name', vm_name,
        '--query', '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'], _out=io)
    ip_address = io.getvalue().strip().replace('"', '')
    print_status('VM IP address is {}'.format(ip_address))
    io.close()
    vm_connect_str = "myuser@{}".format(ip_address)
    my_vm = ssh.bake(['-oStrictHostKeyChecking=no', vm_connect_str])
    print_status('Installing git.')
    build_prereqs = "sudo yum update -y && sudo yum install -y git gcc rpm-build rpm-devel rpmlint make python bash coreutils " \
                    "diffutils patch rpmdevtools python libffi-devel python-devel openssl-devel"
    my_vm(build_prereqs.split(),
          _out=sys.stdout, _err=sys.stdout)
    my_vm("mkdir -p ~/rpmbuild/BUILD ~/rpmbuild/RPMS ~/rpmbuild/SOURCES ~/rpmbuild/SPECS ~/rpmbuild/SRPMS".split(), _out=io)
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    repo_dir = io.getvalue().strip()
    io.close()
    print_status('Cloning repo.')
    my_vm(['git', 'clone', 'https://github.com/{}'.format(script_env.get('REPO_NAME')), repo_dir], _out=sys.stdout, _err=sys.stdout)
    path_to_spec_file = os.path.join(repo_dir, 'packaged_releases', 'rpm', 'azure-cli.spec')
    print_status('Running build script.')
    my_vm(['export', 'CLI_VERSION={}'.format(script_env.get('CLI_VERSION')), '&&',
           'export', 'CLI_DOWNLOAD_SHA256={}'.format(script_env.get('CLI_DOWNLOAD_SHA256')), '&&',
           'rpmbuild', '-v', '-bb', '--clean', path_to_spec_file],
          _out=sys.stdout, _err=sys.stdout)
    print_status('Build complete.')
    io = StringIO()
    my_vm(['ls', '~/rpmbuild/RPMS/*/*'], _out=io)
    rpm_file_path = io.getvalue().strip()
    io.close()
    artifact_name = rpm_file_path.split('/')[-1]
    print_status('Installing the .rpm on the build machine')
    my_vm(['sudo', 'rpm', '-i', rpm_file_path], _out=sys.stdout, _err=sys.stdout)
    # Upload to Azure Storage
    print_status('Uploading .rpm to Azure storage.')
    my_vm(['az', 'storage', 'container', 'create', '--name', 'rpms', '--public-access', 'blob',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    my_vm(['az', 'storage', 'blob', 'upload', '-f', rpm_file_path,
           '-n', artifact_name, '-c', 'rpms', '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    my_vm(['az', 'storage', 'blob', 'url', '-n', artifact_name, '-c', 'rpms', '--output', 'tsv',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))], _out=io)
    rpm_url = io.getvalue().strip()
    io.close()
    print_status('RPM file uploaded to the following URL.')
    print_status(rpm_url)
    # Publish to service
    my_vm(['wget', '-q', 'https://bootstrap.pypa.io/get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'python', 'get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'pip', 'install', '--upgrade', 'requests'], _out=sys.stdout, _err=sys.stdout)
    upload_script = REPO_UPLOAD_SCRIPT_TMPL.format(cli_version=script_env.get('CLI_VERSION'),
                                                   repo_id=script_env.get('YUM_REPO_ID'),
                                                   source_url=rpm_url,
                                                   repo_package_url=script_env.get('MS_REPO_URL'),
                                                   repo_user=script_env.get('MS_REPO_USERNAME'),
                                                   repo_pass=script_env.get('MS_REPO_PASSWORD'))
    my_vm(['echo', '-e', '"{}"'.format(upload_script), '>>', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    # Keeping this code commented for when we can automate the signing of RPM packages.
    # my_vm(['python', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status('PRINTING OUT REPO UPLOAD SCRIPT AS THE UNSIGNED RPM NEEDS TO BE FIRST SIGNED BEFORE UPLOADING...')
    my_vm(['cat', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status('Done. :)')
    give_chance_to_cancel('Delete resource group (in background)')
    az(['group', 'delete', '--name', resource_group, '--yes', '--no-wait'], _out=sys.stdout, _err=sys.stdout)
    print_status('Finished. :)')
Ejemplo n.º 7
0
def main():
    print_env_vars()
    time_str = datetime.utcnow().strftime('%Y%m%d%H%M%S')
    az(["login"], _out=sys.stdout, _err=sys.stdout)
    resource_group = 'azurecli-release-debian-' + time_str
    vm_name = 'vm-debian-' + time_str
    print_status('Creating resource group.')
    az(['group', 'create', '-l', 'westus', '-n', resource_group], _out=sys.stdout, _err=sys.stdout)
    print_status('Creating VM.')
    az(['vm', 'create', '-g', resource_group, '-n', vm_name, '--generate-ssh-keys', '--authentication-type', 'ssh',
        '--image', 'Canonical:UbuntuServer:14.04.4-LTS:latest', '--admin-username', 'ubuntu'],
       _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    print_status('Getting VM IP address.')
    az(['vm', 'list-ip-addresses', '--resource-group', resource_group, '--name', vm_name,
        '--query', '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'], _out=io)
    ip_address = io.getvalue().strip().replace('"', '')
    print_status('VM IP address is {}'.format(ip_address))
    io.close()
    vm_connect_str = "ubuntu@{}".format(ip_address)
    my_vm = ssh.bake(['-oStrictHostKeyChecking=no', vm_connect_str])
    print_status('Installing git.')
    my_vm(['sudo', 'apt-get', 'update', '&&', 'sudo', 'apt-get', 'install', '-y', 'git'],
          _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    repo_dir = io.getvalue().strip()
    io.close()
    print_status('Cloning repo.')
    my_vm(['git', 'clone', 'https://github.com/{}'.format(script_env.get('REPO_NAME')), repo_dir], _out=sys.stdout, _err=sys.stdout)
    path_to_debian_build_script = os.path.join(repo_dir, 'packaged_releases', 'debian', 'debian_build.sh')
    path_to_dir_creator = os.path.join(repo_dir, 'packaged_releases', 'debian', 'debian_dir_creator.sh')
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    build_artifact_dir = io.getvalue().strip()
    io.close()
    print_status('Running debian build scripts.')
    my_vm(['chmod', '+x', path_to_debian_build_script, path_to_dir_creator], _out=sys.stdout, _err=sys.stdout)
    my_vm(['export', 'CLI_VERSION={}'.format(script_env.get('CLI_VERSION')), '&&',
           'export', 'CLI_DOWNLOAD_SHA256={}'.format(script_env.get('CLI_DOWNLOAD_SHA256')), '&&',
           'export', 'BUILD_ARTIFACT_DIR={}'.format(build_artifact_dir), '&&',
           path_to_debian_build_script, path_to_dir_creator],
          _out=sys.stdout, _err=sys.stdout)
    print_status('Debian build complete.')
    io = StringIO()
    my_vm(['ls', build_artifact_dir], _out=io)
    artifact_name = io.getvalue().strip()
    io.close()
    deb_file_path = os.path.join(build_artifact_dir, artifact_name)
    print_status('Installing the .deb on the build machine')
    my_vm(['sudo', 'dpkg', '-i', deb_file_path], _out=sys.stdout, _err=sys.stdout)
    # Upload to Azure Storage
    print_status('Uploading .deb to Azure storage.')
    my_vm(['az', 'storage', 'container', 'create', '--name', 'repos', '--public-access', 'blob',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    my_vm(['az', 'storage', 'blob', 'upload', '-f', deb_file_path,
           '-n', artifact_name, '-c', 'repos', '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))],
          _out=sys.stdout, _err=sys.stdout)
    io = StringIO()
    my_vm(['az', 'storage', 'blob', 'url', '-n', artifact_name, '-c', 'repos', '--output', 'tsv',
           '--connection-string', '"{}"'.format(script_env.get('AZURE_STORAGE_CONNECTION_STRING'))], _out=io)
    deb_url = io.getvalue().strip()
    io.close()
    print_status('Debian file uploaded to the following URL.')
    print_status(deb_url)
    # Publish to apt service
    my_vm(['wget', '-q', 'https://bootstrap.pypa.io/get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'python', 'get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'pip', 'install', '--upgrade', 'requests'], _out=sys.stdout, _err=sys.stdout)
    upload_script = REPO_UPLOAD_SCRIPT_TMPL.format(cli_version=script_env.get('CLI_VERSION'),
                                                   repo_id=script_env.get('DEBIAN_REPO_ID'),
                                                   source_url=deb_url,
                                                   repo_package_url=script_env.get('MS_REPO_URL'),
                                                   repo_user=script_env.get('MS_REPO_USERNAME'),
                                                   repo_pass=script_env.get('MS_REPO_PASSWORD'))
    my_vm(['echo', '-e', '"{}"'.format(upload_script), '>>', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['python', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status('Done. :)')
    give_chance_to_cancel('Delete resource group (in background)')
    az(['group', 'delete', '--name', resource_group, '--yes', '--no-wait'], _out=sys.stdout, _err=sys.stdout)
    print_status('Finished. :)')
def main():
    print_env_vars()
    time_str = datetime.utcnow().strftime('%Y%m%d%H%M%S')
    az(["login"], _out=sys.stdout, _err=sys.stdout)
    resource_group = 'azurecli-release-rpm-' + time_str
    vm_name = 'vm-rpm-' + time_str
    print_status('Creating resource group.')
    az(['group', 'create', '-l', 'westus', '-n', resource_group],
       _out=sys.stdout,
       _err=sys.stdout)
    print_status('Creating VM.')
    az([
        'vm', 'create', '-g', resource_group, '-n', vm_name,
        '--generate-ssh-keys', '--authentication-type', 'ssh', '--image',
        'OpenLogic:CentOS:7.3:latest', '--admin-username', 'myuser'
    ],
       _out=sys.stdout,
       _err=sys.stdout)
    io = StringIO()
    print_status('Getting VM IP address.')
    az([
        'vm', 'list-ip-addresses', '--resource-group', resource_group,
        '--name', vm_name, '--query',
        '[0].virtualMachine.network.publicIpAddresses[0].ipAddress'
    ],
       _out=io)
    ip_address = io.getvalue().strip().replace('"', '')
    print_status('VM IP address is {}'.format(ip_address))
    io.close()
    vm_connect_str = "myuser@{}".format(ip_address)
    my_vm = ssh.bake(['-oStrictHostKeyChecking=no', vm_connect_str])
    print_status('Installing git.')
    build_prereqs = "sudo yum update -y && sudo yum install -y git gcc rpm-build rpm-devel rpmlint make python bash coreutils " \
                    "diffutils patch rpmdevtools python libffi-devel python-devel openssl-devel"
    my_vm(build_prereqs.split(), _out=sys.stdout, _err=sys.stdout)
    my_vm(
        "mkdir -p ~/rpmbuild/BUILD ~/rpmbuild/RPMS ~/rpmbuild/SOURCES ~/rpmbuild/SPECS ~/rpmbuild/SRPMS"
        .split(),
        _out=io)
    io = StringIO()
    my_vm(['mktemp', '-d'], _out=io)
    repo_dir = io.getvalue().strip()
    io.close()
    print_status('Cloning repo.')
    my_vm([
        'git', 'clone', 'https://github.com/{}'.format(
            script_env.get('REPO_NAME')), repo_dir
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    path_to_spec_file = os.path.join(repo_dir, 'packaged_releases', 'rpm',
                                     'azure-cli.spec')
    print_status('Running build script.')
    my_vm([
        'export', 'CLI_VERSION={}'.format(script_env.get('CLI_VERSION')), '&&',
        'export', 'CLI_DOWNLOAD_SHA256={}'.format(
            script_env.get('CLI_DOWNLOAD_SHA256')), '&&', 'rpmbuild', '-v',
        '-bb', '--clean', path_to_spec_file
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    print_status('Build complete.')
    io = StringIO()
    my_vm(['ls', '~/rpmbuild/RPMS/*/*'], _out=io)
    rpm_file_path = io.getvalue().strip()
    io.close()
    artifact_name = rpm_file_path.split('/')[-1]
    print_status('Installing the .rpm on the build machine')
    my_vm(['sudo', 'rpm', '-i', rpm_file_path],
          _out=sys.stdout,
          _err=sys.stdout)
    # Upload to Azure Storage
    print_status('Uploading .rpm to Azure storage.')
    my_vm([
        'az', 'storage', 'container', 'create', '--name', 'rpms',
        '--public-access', 'blob', '--connection-string', '"{}"'.format(
            script_env.get('AZURE_STORAGE_CONNECTION_STRING'))
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    my_vm([
        'az', 'storage', 'blob', 'upload', '-f', rpm_file_path, '-n',
        artifact_name, '-c', 'rpms', '--connection-string', '"{}"'.format(
            script_env.get('AZURE_STORAGE_CONNECTION_STRING'))
    ],
          _out=sys.stdout,
          _err=sys.stdout)
    io = StringIO()
    my_vm([
        'az', 'storage', 'blob', 'url', '-n', artifact_name, '-c', 'rpms',
        '--output', 'tsv', '--connection-string', '"{}"'.format(
            script_env.get('AZURE_STORAGE_CONNECTION_STRING'))
    ],
          _out=io)
    rpm_url = io.getvalue().strip()
    io.close()
    print_status('RPM file uploaded to the following URL.')
    print_status(rpm_url)
    # Publish to service
    my_vm(['wget', '-q', 'https://bootstrap.pypa.io/get-pip.py'],
          _out=sys.stdout,
          _err=sys.stdout)
    my_vm(['sudo', 'python', 'get-pip.py'], _out=sys.stdout, _err=sys.stdout)
    my_vm(['sudo', 'pip', 'install', '--upgrade', 'requests'],
          _out=sys.stdout,
          _err=sys.stdout)
    upload_script = REPO_UPLOAD_SCRIPT_TMPL.format(
        cli_version=script_env.get('CLI_VERSION'),
        repo_id=script_env.get('YUM_REPO_ID'),
        source_url=rpm_url,
        repo_package_url=script_env.get('MS_REPO_URL'),
        repo_user=script_env.get('MS_REPO_USERNAME'),
        repo_pass=script_env.get('MS_REPO_PASSWORD'))
    my_vm(['echo', '-e', '"{}"'.format(upload_script), '>>', 'repo_upload.py'],
          _out=sys.stdout,
          _err=sys.stdout)
    # Keeping this code commented for when we can automate the signing of RPM packages.
    # my_vm(['python', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status(
        'PRINTING OUT REPO UPLOAD SCRIPT AS THE UNSIGNED RPM NEEDS TO BE FIRST SIGNED BEFORE UPLOADING...'
    )
    my_vm(['cat', 'repo_upload.py'], _out=sys.stdout, _err=sys.stdout)
    print_status('Done. :)')
    give_chance_to_cancel('Delete resource group (in background)')
    az(['group', 'delete', '--name', resource_group, '--yes', '--no-wait'],
       _out=sys.stdout,
       _err=sys.stdout)
    print_status('Finished. :)')