#!/usr/bin/env ruby require 'bundler/setup' require 'nutkins' require 'nutkins/version' require 'moister' require 'ostruct' module Nutkins::Command def self.run args global_config = nil command = nil Moister::SubcommandOptionParser.new do |op| op.banner = "usage: nutkins #{Nutkins::VERSION} [global options] command [command options]" op.version = Nutkins::VERSION op.for_all do |op| op.on_tail '-h', '--help', 'show this help message' do puts op exit end end op.on '-p', '--project dir', 'override path to project', 'project_dir' op.subcommand 'build,b *paths', 'build docker image[s] from nutkin.yamls/dockerfiles' op.subcommand 'create,c *paths', 'create container from image' do |subop| subop.on '-r', '--reuse', 'reuse previously built image', 'reuse' subop.on '-p', '--preserve', 'preserve existing container', 'preserve' end op.subcommand 'delete,d *paths', 'delete container corresponding to image' op.subcommand 'delete-all', 'delete containers corresponding to all images' op.subcommand 'run,r path', 'run created container' do |subop| subop.on '-r', '--reuse', 'reuse previously created container', 'reuse' subop.on '-s', '--shell', 'add bash shell to console', 'shell' end op.subcommand 'exec,e path *cmd', 'execute a command in a running container' op.subcommand 'build-secret,B path', 'build secret files/volumes' op.subcommand 'extract-secrets,X [*paths]', 'extract secret files/volumes' op.subcommand 'start-etcd', 'start container running etcd' op.subcommand 'stop-etcd', 'stop container running etcd' parsed_cfg = op.parse(args).to_h global_config = OpenStruct.new parsed_cfg[:config] command = parsed_cfg[:command] end unless command puts 'please supply a command, see --help' exit 1 end nutkins = Nutkins::CloudManager.new(project_dir: global_config.project_dir) config = OpenStruct.new global_config[command] case command when 'build' config.paths.each &nutkins.method(:build) when 'create' config.paths.each do |path| nutkins.create path, preserve: config.preserve, reuse: config.reuse end when 'delete' config.paths.each &nutkins.method(:delete) when 'delete-all' nutkins.delete_all when 'run' nutkins.run config.path, reuse: config.reuse, shell: config.shell when 'exec' nutkins.exec config.path, config.cmd when 'build-secret' nutkins.build_secret config.path when 'extract-secrets' nutkins.extract_secrets config.paths when 'start-etcd' nutkins.start_etcd_container when 'stop-etcd' nutkins.stop_etcd_container end rescue RuntimeError => e puts e.to_s end end Nutkins::Command::run ARGV