lib/cide.rb in cide-0.0.7 vs lib/cide.rb in cide-0.0.8

- old
+ new

@@ -10,15 +10,21 @@ # CIDE is a Continuous Integration Docker Environment runner # # The juicy bits are defined in CIDE::CLI module CIDE DOCKERFILE = 'Dockerfile' - TEMPLATE = File.read(File.expand_path('../cide_template.erb', __FILE__)) + SSH_CONFIG_FILE = 'ssh_config' + TEMP_SSH_KEY = 'id_rsa.tmp' + DOCKERFILE_TEMPLATE = File.read( + File.expand_path('../cide_template.erb', __FILE__), + ) + SSH_CONFIG_CONTENTS = File.read(File.expand_path('../ssh_config', __FILE__)) CONFIG_FILE = '.cide.yml' CIDE_DIR = '/cide' CIDE_SRC_DIR = File.join(CIDE_DIR, '/src') + CIDE_SSH_DIR = File.join(CIDE_DIR, '/.ssh') module_function def docker_id(str) # Replaces invalid docker tag characters by underscores @@ -37,21 +43,22 @@ before: {}, export: false, export_dir: './artifacts', host_export_dir: nil, run: 'script/ci', + ssh_key: nil, ) do alias_method :image=, :from= alias_method :command=, :run= def name=(str) super CIDE.docker_id(str) end def to_dockerfile - ERB.new(TEMPLATE, nil, '<>-').result(binding) + ERB.new(DOCKERFILE_TEMPLATE, nil, '<>-').result(binding) end def merge!(opts = {}) opts.each_pair { |k, v| public_send("#{k}=", v) } self @@ -96,10 +103,15 @@ method_option 'run', desc: 'The script to run', aliases: ['r'], default: nil + method_option 'ssh_key', + desc: 'The ssh key to put into the docker image', + aliases: ['s'], + default: nil + def build setup_docker config = DefaultConfig.merge YAML.load_file(CONFIG_FILE) options.each_pair do |k, v| @@ -108,10 +120,22 @@ config.name ||= File.basename(Dir.pwd) config.host_export_dir ||= config.export_dir tag = "cide/#{config.name}" + if config.ssh_key && File.exist?(config.ssh_key) + say_status :SSHkey, 'Creating temp ssh key file within directory' + ssh_key_contents = File.read(config.ssh_key) + File.write(TEMP_SSH_KEY, ssh_key_contents) + config.ssh_key = TEMP_SSH_KEY + at_exit do + File.unlink(TEMP_SSH_KEY) + end + else + say_status :SSHKey, 'No SSH key specified' + end + say_status :config, config.to_h # FIXME: Move Dockerfile out of the way if it exists if !File.exist?(DOCKERFILE) say_status :Dockerfile, 'Creating temporary Dockerfile' @@ -119,9 +143,19 @@ at_exit do File.unlink(DOCKERFILE) end else say_status :Dockerfile, 'Using existing Dockerfile' + end + + if !File.exist?(SSH_CONFIG_FILE) + say_status :ssh_config, 'Creating temporary ssh config' + File.write(SSH_CONFIG_FILE, SSH_CONFIG_CONTENTS) + at_exit do + File.unlink(SSH_CONFIG_FILE) + end + else + say_status :ssh_config, 'Using existing ssh config' end docker :build, '-t', tag, '.' return unless config.export