Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #103 from emre/issue-99
Browse files Browse the repository at this point in the history
Add clone and backup commands
  • Loading branch information
emre committed Oct 1, 2015
2 parents de34cbc + 8e280f3 commit 41ec1aa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='stormssh',
version='0.6.5',
version='0.6.6',
packages=find_packages(),
package_data={'storm': ['templates/*.html', 'static/css/*.css',
'static/css/themes/storm/*.css', 'static/css/themes/storm/img/*.png',
Expand Down
9 changes: 7 additions & 2 deletions storm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from operator import itemgetter
import re
from shutil import copyfile

from .parsers.ssh_config_parser import ConfigParser
from .defaults import get_default
Expand Down Expand Up @@ -36,8 +37,7 @@ def add_entry(self, name, host, user, port, id_file, custom_options=[]):

return True


def clone_entry(self, name, clone_name):
def clone_entry(self, name, clone_name, keep_original=True):
host = self.is_host_in(name, return_match=True)
if not host:
raise ValueError(ERRORS["not_found"].format(name))
Expand All @@ -47,6 +47,8 @@ def clone_entry(self, name, clone_name):
raise ValueError(ERRORS["already_in"].format(clone_name))

self.ssh_config.add_host(clone_name, host.get('options'))
if not keep_original:
self.ssh_config.delete_host(name)
self.ssh_config.write_to_ssh_config()

return True
Expand Down Expand Up @@ -140,3 +142,6 @@ def is_host_in(self, host, return_match = False, regexp_match=False):
if host_.get("host") == host or (regexp_match and re.match(host, host_.get("host"))):
return True if not return_match else host_
return False if not return_match else None

def backup(self, target_file):
return copyfile(self.ssh_config.ssh_config_file, target_file)
37 changes: 37 additions & 0 deletions storm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ def clone(name, clone_name, config=None):
except ValueError as error:
print(get_formatted_message(error, 'error'), file=sys.stderr)

@command('move')
def move(name, entry_name, config=None):
"""
Move an entry to the sshconfig.
"""
storm_ = get_storm_instance(config)

try:

# validate name
if '@' in name:
raise ValueError('invalid value: "@" cannot be used in name.')

storm_.clone_entry(name, entry_name, keep_original=False)

print(
get_formatted_message(
'{0} moved in ssh config. you can '
'connect it by typing "ssh {0}".'.format(
entry_name
),
'success')
)

except ValueError as error:
print(get_formatted_message(error, 'error'), file=sys.stderr)


@command('edit')
def edit(name, connection_uri, id_file="", o=[], config=None):
Expand Down Expand Up @@ -234,6 +261,16 @@ def delete_all(config=None):
except Exception as error:
print(get_formatted_message(str(error), 'error'), file=sys.stderr)

@command('backup')
def backup(target_file, config=None):
"""
Backups the main ssh configuration into target file.
"""
storm_ = get_storm_instance(config)
try:
storm_.backup(target_file)
except Exception as error:
print(get_formatted_message(str(error), 'error'), file=sys.stderr)

@command('web')
@arg('port', nargs='?', default=9002, type=int)
Expand Down
36 changes: 35 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ def test_search(self):
self.assertTrue(out.startswith(b'Listing results for aws:'))
self.assertIn(b'aws.apache', out)

def test_backup(self):
out, err, rc = self.run_cmd("backup /tmp/ssh_backup {0}".format(
self.config_arg))

self.assertEqual(True, os.path.exists("/tmp/ssh_backup"))

def test_invalid_search(self):

out, err, rc = self.run_cmd("search THEREISNOTHINGRELATEDWITHME {0}".format(self.config_arg))
Expand All @@ -283,7 +289,6 @@ def test_delete_all(self):

self.assertIn(b'all entries deleted', out)


def tearDown(self):
os.unlink('/tmp/ssh_config_cli_tests')

Expand Down Expand Up @@ -344,6 +349,35 @@ def test_clone_host(self):
self.assertEqual(item.get("options").get("identityfile"), '/tmp/tmp.pub')
self.assertEqual(item.get("options").get("user"), 'ops')

def test_move_host(self):
self.storm.add_entry('google', 'google.com', 'ops', '24', '/tmp/tmp.pub')
self.storm.clone_entry('google', 'yahoo', keep_original=False)

has_yahoo = False
for item in self.storm.ssh_config.config_data:
if item.get("host") == 'yahoo':
has_yahoo = True
break

has_google = False
for item in self.storm.ssh_config.config_data:
if item.get("host") == 'google':
has_google = True
break

self.assertEqual(True, has_yahoo)
self.assertEqual(False, has_google)
self.assertEqual(item.get("options").get("port"), '24')
self.assertEqual(item.get("options").get("identityfile"), '/tmp/tmp.pub')
self.assertEqual(item.get("options").get("user"), 'ops')

def test_backup(self):
self.storm.backup("/tmp/storm_ssh_config_backup_file")
self.assertEqual(
True,
os.path.exists("/tmp/storm_ssh_config_backup_file")
)

def test_double_clone_exception(self):
self.storm.add_entry('google', 'google.com', 'ops', '24', '/tmp/tmp.pub')
self.storm.clone_entry('google', 'yahoo')
Expand Down

0 comments on commit 41ec1aa

Please sign in to comment.