Clone private github repo to launched EC2 instance

Question:

I’m building a scrip that launches and connects to an EC2 instance. 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.

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 "[email protected]" -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" [email protected]: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: [email protected]}

Asked By: Higor Nunes

||

Answers:

Solved this taking a different approach a using github CLI.

Setting the GH TOKEN variable to my token gave the instance the privileges to add the public key to my github account.

print("Creating ssh key pair...")
stdin, stdout, stderr = client.exec_command('ssh-keygen -t rsa -b 4096 -C "[email protected]" -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" [email protected]:myOrganization/'+repository+'.git /home/ec2-user/project')
print(stdout.read().decode())
print(stderr.read().decode())
print("Done")
Answered By: Higor Nunes