简体   繁体   English

克隆私有 github 回购以启动 EC2 实例

[英]Clone private github repo to launched EC2 instance

I'm building a scrip that launches and connects to an EC2 instance.我正在构建一个启动并连接到 EC2 实例的脚本。 After all setup of the instance I have to copy my private repo to it, and then run it.在实例的所有设置之后,我必须将我的私人仓库复制到它,然后运行它。 The code works fine for public repo.该代码适用于公共回购。 Also if I manually launch an instance, connect to it and run line by line, it also works fine.此外,如果我手动启动一个实例,连接到它并逐行运行,它也可以正常工作。 I'm using python, boto3 and paramiko.我正在使用 python、boto3 和 paramiko。

Here is what I have so far:这是我到目前为止所拥有的:

print("Creating ssh key pair...")
    stdin, stdout, stderr = client.exec_command('ssh-keygen -t rsa -b 4096 -C "myemail@gmail.com" -f ~/.ssh/id_rsa -N ""')
    output = stdout.read().decode()
    print(output)
    print("Done")

    # add ssh key to ssh-agent
    print("Adding ssh key to ssh agent")
    stdin, stdout, stderr = client.exec_command('eval "$(ssh-agent -s)" ; ssh-add ~/.ssh/id_rsa')
    output = stdout.read().decode()
    print(output)

    stdin, stdout, stderr = client.exec_command('ssh-keyscan github.com >> ~/.ssh/known_hosts')
    output = stdout.read().decode()
    print(output)

    # add ssh key to github account
    print("Adding ssh key to github account...")
    stdin, stdout, stderr = client.exec_command('curl -u "myusername:mytoken" -H "Content-Type: application/json" --data "{\"title\":\"EC2_Instance_Key\",\"key\":\"$(cat ~/.ssh/id_rsa.pub)\"}" https://api.github.com/user/keys -X POST')
    output = stdout.read().decode()
    print(output)
    print("Done")

    # clone the repository
    print("Cloning the repository to the instance...")
    stdin, stdout, stderr = client.exec_command('git clone -o "StrictHostKeyChecking=no" git@github.com:myOrganization/ec2_test.git /home/ec2-user/project')
    output = stdout.read().decode()
    print(output)
    print("Done")

I am getting this error:我收到此错误:

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: AAAAB3NzaC1yc2EAAAADAQABAAACAQDH50Rl curl: (3) unmatched close brace/bracket in URL position 18: myemail@gmail.com} % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl:(6)无法解析主机:AAAAB3NzaC1yc2EAAAADAQABAAACAQDH50Rl curl:(3)URL position 18 中不匹配的右括号/括号:myemail@gmail.com}

Solved this taking a different approach a using github CLI.使用 github CLI 采取不同的方法解决了这个问题。

Setting the GH TOKEN variable to my token gave the instance the privileges to add the public key to my github account.将 GH TOKEN 变量设置为我的令牌,使实例有权将公钥添加到我的 github 帐户。

print("Creating ssh key pair...")
stdin, stdout, stderr = client.exec_command('ssh-keygen -t rsa -b 4096 -C "myemail@email.com" -f /home/ec2-user/.ssh/id_rsa -N ""')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")

print("Adding ssh key to ssh agent...")
stdin, stdout, stderr = client.exec_command('eval "$(ssh-agent -s)" ; ssh-add /home/ec2-user/.ssh/id_rsa')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")

print("Adding github to known hosts...")
stdin, stdout, stderr = client.exec_command('ssh-keyscan github.com >> /home/ec2-user/.ssh/known_hosts')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")

print("Installing github cli...")
stdin, stdout, stderr = client.exec_command('sudo yum -y install wget ; wget https://github.com/cli/cli/releases/download/v2.15.0/gh_2.15.0_linux_amd64.rpm ; sudo rpm -i gh_2.15.0_linux_amd64.rpm ; gh --version')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")


print("Setting GH TOKEN...")
title = project_id + '_' + instance_id
stdin, stdout, stderr = client.exec_command('export GH_TOKEN=mytoken; echo $GH_TOKEN ; gh ssh-key add /home/ec2-user/.ssh/id_rsa.pub --title '+title)
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")

print("Cloning the repository to the instance...")
stdin, stdout, stderr = client.exec_command('git clone -o "StrictHostKeyChecking=no" git@github.com:myOrganization/'+repository+'.git /home/ec2-user/project')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM