class Fastlane::Actions::CoreosDeployAction

Public Class Methods

authors() click to toggle source
# File lib/fastlane/plugin/coreos/actions/coreos_deploy_action.rb, line 57
def self.authors
  ["Oliver Letterer"]
end
available_options() click to toggle source
# File lib/fastlane/plugin/coreos/actions/coreos_deploy_action.rb, line 61
def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :docker_hub_user,
                            env_name: "COREOS_DOCKER_HUB_USER",
                         description: "User for Docker Hub login",
                            optional: true,
                                type: String
                                ),
    FastlaneCore::ConfigItem.new(key: :docker_hub_password,
                            env_name: "COREOS_DOCKER_HUB_PASSWORD",
                         description: "Password for Docker Hub login",
                            optional: true,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :image,
                            env_name: "COREOS_DOCKER_IMAGE",
                         description: "Name of the docker image to deploy",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :host,
                            env_name: "COREOS_HOST",
                         description: "Domain of the host to deploy to",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :service_file,
                            env_name: "COREOS_SERVICE_FILE",
                         description: "Systemd service file to deploy",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :user,
                            env_name: "COREOS_USER",
                         description: "User to use for login on remote machines. Defaults to $USER",
                            optional: true,
                                type: String),
  ]
end
description() click to toggle source
# File lib/fastlane/plugin/coreos/actions/coreos_deploy_action.rb, line 53
def self.description
  "Deploy docker services to CoreOS hosts"
end
is_supported?(platform) click to toggle source
# File lib/fastlane/plugin/coreos/actions/coreos_deploy_action.rb, line 97
def self.is_supported?(platform)
  true
end
run(params) click to toggle source
# File lib/fastlane/plugin/coreos/actions/coreos_deploy_action.rb, line 4
def self.run(params)
  raise "Service file #{params[:service_file]} does not exist" unless File.exist?(params[:service_file])

  user = params[:user] || ENV["USER"]
  host = params[:host]
  image = params[:image]
  service_name = params[:service_file].split("/").last.gsub(".erb", "")
  file = Helper::CoreosSystemdTemplate.with_params(params)

  sh "scp #{file.path} #{user}@#{host}:#{service_name}"

  Net::SSH.start(host, user) do |ssh|
    UI.success("ssh #{user}@#{host}")

    docker_hub_user = params[:docker_hub_user]
    docker_hub_password = params[:docker_hub_password]

    # login
    if docker_hub_user and docker_hub_password
      UI.message "Performing docker login for #{docker_hub_user}"
      ssh.exec! "docker login -u #{docker_hub_user} -p #{docker_hub_password}"
      ssh.loop
      UI.success("You are now logged in as #{docker_hub_user}")
    end
  end

  login = "#{user}@#{host}"

  ssh = Proc.new { |command|
    sh %{ssh #{login} "#{command}"}
  }

  # pull image
  ssh.call "docker pull #{image}"

  existed = ssh.call("test -e /etc/systemd/system/#{service_name} && echo 1 || echo 0").to_i
  ssh.call "sudo mv #{service_name} /etc/systemd/system/"

  unless existed
    # start the service
    ssh.call "sudo systemctl enable /etc/systemd/system/#{service_name}"
    ssh.call "sudo systemctl start #{service_name}"
  else
    # restart service
    ssh.call "sudo systemctl daemon-reload"
    ssh.call "sudo systemctl restart #{service_name}"
  end
end