lib/gearship/cli.rb in gearship-0.2.3 vs lib/gearship/cli.rb in gearship-0.2.4

- old
+ new

@@ -5,20 +5,26 @@ module Gearship class Cli < Thor include Thor::Actions desc 'init', 'Initialize gearship project' - def init(project = 'gearship') - init!(project) + def init + init!('gearship') end desc 'go [mission] [--sudo]', 'Send gearship on a mission with ssh.' method_options :sudo => false - def go(target, *args) - go!(target, *args) + def go(mission, *args) + go!(mission, *args) end + desc 'local [mission] [--sudo]', 'Send gearship on a local mission.' + method_options :sudo => false + def local(mission, *args) + local!(mission, *args) + end + desc 'compile', 'Compile gearship project for debugging' def compile(mission = nil) compile!(mission) end @@ -28,40 +34,41 @@ def self.source_root File.expand_path('../../',__FILE__) end def init!(project) - copy_file 'templates/.dockerignore', ".dockerignore" - copy_file 'templates/.gitignore', "#{project}/.gitignore" - copy_file 'templates/gearship.yml', "#{project}/gearship.yml" - copy_file 'templates/gearship.sh', "#{project}/gearship.sh" + copy_file 'templates/.dockerignore', ".dockerignore" + copy_file 'templates/.gitignore', "gearship/.gitignore" + copy_file 'templates/gearship.yml', "gearship/gearship.yml" + copy_file 'templates/gearship.sh', "gearship/gearship.sh" - copy_file 'templates/actions/configure_firewall.sh', "#{project}/actions/configure_firewall.sh" - copy_file 'templates/actions/install_basics.sh', "#{project}/actions/install_basics.sh" - copy_file 'templates/actions/install_docker.sh', "#{project}/actions/install_docker.sh" - copy_file 'templates/actions/pull_latest_image.sh', "#{project}/actions/pull_latest_image.sh" - copy_file 'templates/actions/remove_container.sh', "#{project}/actions/remove_container.sh" - copy_file 'templates/actions/start_container.sh', "#{project}/actions/start_container.sh" + copy_file 'templates/actions/configure_firewall.sh', "gearship/actions/configure_firewall.sh" + copy_file 'templates/actions/install_basics.sh', "gearship/actions/install_basics.sh" + copy_file 'templates/actions/install_docker.sh', "gearship/actions/install_docker.sh" + copy_file 'templates/actions/login_docker.sh', "gearship/actions/login_docker.sh" + copy_file 'templates/actions/pull_image.sh', "gearship/actions/pull_image.sh" + copy_file 'templates/actions/remove_container.sh', "gearship/actions/remove_container.sh" + copy_file 'templates/actions/start_container.sh', "gearship/actions/start_container.sh" - copy_file 'templates/missions/install_container.sh', "#{project}/missions/install_container.sh" - copy_file 'templates/missions/setup_host.sh', "#{project}/missions/setup_host.sh" - copy_file 'templates/missions/update_container.sh', "#{project}/missions/update_container.sh" + copy_file 'templates/missions/install_container.sh', "gearship/missions/install_container.sh" + copy_file 'templates/missions/setup_host.sh', "gearship/missions/setup_host.sh" + copy_file 'templates/missions/update_container.sh', "gearship/missions/update_container.sh" - copy_file 'templates/cargo/sample.conf', "#{project}/cargo/sample.conf" + copy_file 'templates/cargo/sample.conf', "gearship/cargo/sample.conf" end def go!(*args) mission = args[0] compile!(mission) sudo = 'sudo ' if options.sudo? if mission.include? 'local' local_commands = <<-EOS - bash compiled/gearship.sh + cd compiled + #{sudo}bash gearship.sh EOS else - user, host, port = parse_target(@config['attributes']['ssh_target']) endpoint = "#{user}@#{host}" # Remove server key from known hosts to avoid mismatch errors when VMs change. `ssh-keygen -R #{host} 2> /dev/null` @@ -80,10 +87,27 @@ cd compiled tar cz . | ssh -o 'StrictHostKeyChecking no' #{endpoint} -p #{port} '#{remote_commands}' EOS end + execute(local_commands) + end + + def local!(*args) + mission = args[0] + compile!(mission) + sudo = 'sudo ' if options.sudo? + + local_commands = <<-EOS + cd compiled + #{sudo}bash gearship.sh + EOS + + execute(local_commands) + end + + def execute(local_commands) Open3.popen3(local_commands) do |stdin, stdout, stderr| stdin.close t = Thread.new do while (line = stderr.gets) print line.color(:red) @@ -93,10 +117,9 @@ print line.color(:green) end t.join end end - def compile!(mission) abort_with 'You must be in the gearship folder' unless File.exists?('gearship.yml') abort_with "#{mission} doesn't exist!" if mission and !File.exists?("missions/#{mission}.sh")