Example #1
0
    def __init__(self, name=None):
        """Create a new key without passphrase if there is any."""
        if not name:
            name = ".iscsi_fake_id_dsa"

        self.name = name

        if not os.path.exists(self.name):
            sh.ssh_keygen("-t", "dsa", "-f", self.name, "-N", "")

        os.chmod(self.key(), 0600)
        os.chmod(self.pub_key(), 0600)
Example #2
0
    def __init__(self, name=None):
        """Create a new key without passphrase if there is any."""
        if not name:
            name = '.iscsi_fake_id_dsa'

        self.name = name

        if not os.path.exists(self.name):
            sh.ssh_keygen('-t', 'dsa', '-f', self.name, '-N', '')

        os.chmod(self.key(), 0600)
        os.chmod(self.pub_key(), 0600)
Example #3
0
    def __init__(self, name=None):
        """Create a new key without passphrase if there is any."""
        if not name:
            name = '.iscsi_fake_id_dsa'

        self.name = name

        if not os.path.exists(self.name):
            sh.ssh_keygen('-t', 'dsa', '-f', self.name, '-N', '')

        os.chmod(self.key(), 0o600)
        os.chmod(self.pub_key(), 0o600)
Example #4
0
def setup_ssh(ssh_key: str):
    print("\n === Setting up SSH === ")
    apti("openssh-server")

    Path("/home/ubuntu/.ssh").mkdir(mode=0o700, exist_ok=True)
    shutil.chown("/home/ubuntu/.ssh", "ubuntu")

    if not Path("/home/ubuntu/.ssh/id_rsa").exists():
        print("Generating SSH key...")
        ssh_keygen("-t",
                   "rsa",
                   "-N",
                   "",
                   "-f",
                   "/home/ubuntu/.ssh/id_rsa",
                   _uid=os.getuid())
        shutil.chown("/home/ubuntu/.ssh/id_rsa", "ubuntu")
        shutil.chown("/home/ubuntu/.ssh/id_rsa.pub", "ubuntu")

    print("Importing public launchpad key...")
    sh.ssh_import_id(ssh_key)
Example #5
0
    def keygen(keytype='rsa', bits=4096, comment='', passphrase=''):
        ''' generates an ssh key, returns a (priv, pub) tuple. '''
        # We have to manage two files here, so just handle the files manually
        tmpdir = tempfile.mkdtemp(prefix='keygen')
        try:
            priv_path = os.path.join(tmpdir, 'key')
            pub_path = priv_path + '.pub'

            sh.ssh_keygen(t=keytype,
                          b=bits,
                          q=True,
                          C=comment,
                          N=passphrase,
                          f=priv_path,
                          _in=os.devnull,
                          _tty_in=False,
                          _tty_out=False)
            with open(priv_path, 'r') as priv, open(pub_path, 'r') as pub:
                return priv.read(), pub.read()

        finally:
            FileUtils.ensure_removed(tmpdir)
Example #6
0
def list_vm():
    blob = lxc.list().split('\n')[3:-2]
    blob = [[a.strip() for a in line.split('|')[1:-1]]
            for line in blob]
    blob = dict([(c[0], tuple(c[1:])) for c in blob])
    return blob

vms = list_vm()

for vm in sys.argv[1:]:
    if vm in vms:
        lxc.delete(vm)

    if os.path.exists('home/vagrant/.ssh/known_hosts'):
        ssh_keygen('-f', "/home/vagrant/.ssh/known_hosts", '-R', vm)
    lxc.launch('images:debian/wheezy/amd64', vm, '-p', 'twoNets')

time.sleep(10)
print list_vm()

env = Environment(loader=FileSystemLoader('.'))

names = []

for vm, info in list_vm().items():
    print info
    id = info[1].split('.')[-1]
    tpl = env.get_template('interfaces.j2')
    tpl.stream(id=id).dump(open('interfaces.tmp', 'w'))
    lxc.file.push('--uid=100000', '--gid=100000', 'interfaces.tmp',
 def generate_pubkey(self):
     if not os.path.exists(self.filepath):
         sh.ssh_keygen('-t', self.algorithm, '-P', self.key_password, '-f',
                       self.filepath)
Example #8
0
def gen_ssh_identity_file():
    f = os.path.expanduser("~/.ssh/id_rsa")
    if not os.path.exists(f):
        f = tempfile.mkdtemp("testing-ssh") + "/id_rsa"
        sh.ssh_keygen(b=2048, t="rsa", f=f, N="", q=True)
    return f
Example #9
0
def gen_ssh_identity_file():
    f = tempfile.mkdtemp("testing-ssh") + "/id_rsa"
    sh.ssh_keygen(b=2048, t="rsa", f=f, N="", q=True)
    return f
Example #10
0
def gen_ssh_identity_file():
    f = os.path.expanduser("~/.ssh/id_rsa")
    if not os.path.exists(f):
        f = tempfile.mkdtemp("testing-ssh") + "/id_rsa"
        sh.ssh_keygen(b=2048, t="rsa", f=f, N="", q=True)
    return f
Example #11
0
def list_vm():
    blob = lxc.list().split('\n')[3:-2]
    blob = [[a.strip() for a in line.split('|')[1:-1]] for line in blob]
    blob = dict([(c[0], tuple(c[1:])) for c in blob])
    return blob


vms = list_vm()

for vm in sys.argv[1:]:
    if vm in vms:
        lxc.delete(vm)

    if os.path.exists('home/vagrant/.ssh/known_hosts'):
        ssh_keygen('-f', "/home/vagrant/.ssh/known_hosts", '-R', vm)
    lxc.launch('images:debian/wheezy/amd64', vm, '-p', 'twoNets')

time.sleep(10)
print list_vm()

env = Environment(loader=FileSystemLoader('.'))

names = []

for vm, info in list_vm().items():
    print info
    id = info[1].split('.')[-1]
    tpl = env.get_template('interfaces.j2')
    tpl.stream(id=id).dump(open('interfaces.tmp', 'w'))
    lxc.file.push('--uid=100000', '--gid=100000', 'interfaces.tmp',
Example #12
0
if salt_mapping.api_mode == 'SALTTESTDOCKER':
    logger.debug(
        "Creating intermediate SSH key for accessing the mock salt-master")

    if not os.path.exists(tmp_dir):
        try:
            os.makedirs(tmp_dir, 0o700)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise

    id_rsa_pub = tmp_dir + 'id_rsa.pub'
    if os.path.exists(id_rsa):
        sh.rm(id_rsa)

    sh.ssh_keygen("-t", "rsa", "-f", id_rsa, _in="\n")
    rx_localstore = RxLocalStore()
    connection_details = ConnectionDetails(host_username, host_password,
                                           salt_master, False, ssh_port)
    ssh_login = SSHWrapper.with_connection_details(connection_details)
    ssh_login.send_blocking_command('mkdir /root/.ssh')
    ssh_login.send_file(id_rsa_pub, '/root/.ssh/authorized_keys')
    ssh_connection_details = ConnectionDetails.\
        new_connection_with_custom_key(host_username, host_password, salt_master, id_rsa, ssh_port)
elif salt_mapping.api_mode == 'PRODUCTION':
    id_rsa = LocalSettings.localconfig + '/id_rsa'
    ssh_connection_details = ConnectionDetails.new_connection_with_custom_key(
        remote_user, '', salt_master, id_rsa, ssh_port)

if salt_mapping.api_mode == 'SALTTESTVIRT' or salt_mapping.api_mode == 'SALTTESTDOCKER' or salt_mapping.api_mode == 'PRODUCTION':
Example #13
0
def create_container(vm_name, os, release, arch, config_name):
    if path.exists('home/vagrant/.ssh/known_hosts'):
        ssh_keygen('-f', "/home/vagrant/.ssh/known_hosts", '-R', vm_name)
    image_uri = "images:{os}/{release}/{arch}".format(**vars(args))
    lxc.launch(image_uri, vm_name, '-p', config_name)
    wait_for_vms(vm_name)