Help us understand the problem. What is going on with this article?

ssh してコマンド実行する definitions

More than 5 years have passed since last update.

作ってしまった…

definitions/execute_on.rb
class ::Chef
  class Definition
    # execute command on a host via ssh
    #
    # HOW TO USE:
    #
    # 1)
    #
    #     execute_on "#{name}" do
    #       host "#{host}"
    #       command "#{command}"
    #     end
    #
    # 2) notifies must specify `execute` not `execute_on`
    #
    #     cookbook_file "/tmp/foo" do
    #       source "tmp/foo"
    #       notifies :run, "execute[#{name}]"
    #     end
    #     
    #     execute_on "#{name}" do
    #       host "#{host}"
    #       command "#{command}"
    #     end
    #
    # OPTIONS:
    #
    # Other options are same with `execute` resource.
    # See https://docs.chef.io/resource_execute.html
    module ExecuteOn
      # ssh command
      def ssh_command
        @ssh_command ||= [
          'ssh -S none -T -n',
          '-o BatchMode\ yes',
          '-o ConnectTimeout\ 5',
          '-o StrictHostKeyChecking\ no',
          '-o UserKnownHostsFile\ /dev/null',
          '-o LogLevel\ ERROR',
        ].join(' ')
      end
      module_function :ssh_command

      # build ssh command
      #
      # @param [String] ssh_command Default is self.ssh_command
      # @param [String] host
      # @param [String] command
      # @return [String] built ssh command
      def build_command(ssh_command: nil, host: , command: )
        ssh_command ||= self.ssh_command
        "#{ssh_command} #{host} #{Shellwords.escape(command)}"
      end
      module_function :build_command
    end
  end
end

opts = {
  :ssh_command => ::Chef::Definition::ExecuteOn.ssh_command,
  :host => nil,
}

define :execute_on, opts do
  raise ArgumentError, "`host` is missing for execute_on" unless params[:host]
  name = params[:name]
  command = params[:command] || params[:name]
  ssh_command = params[:ssh_command]
  execute_params = params.dup.tap do |params|
    params.delete(:host)
    params.delete(:name)
    params.delete(:commond)
    params.delete(:ssh_command)
  end
  command = command.call if command.is_a?(Chef::DelayedEvaluator)
  execute_params[:command] = ::Chef::Definition::ExecuteOn.build_command(
    ssh_command: ssh_command,
    host: params[:host],
    command: command,
  )
  execute name do
    execute_params.each {|key, val| send(key, *val) }
  end
end

作ったものの ssh_command を作るヘルパーメソッドだけあれば definitions まではいらない気がしてきている。そもそも chef で他のサーバに入って何かするとかやるなっていう。

sonots
A Ruby, Fluentd, and Chainer Committer. SRE Engineer. Qiitaは小ネタの投稿場所として利用しています。業務コードで、なぜそういう書き方をしているのか解説をQiitaに書いて、コードにはQiitaへのリンクを張る、という使い方をしていることが多いです(自己紹介じゃない)
https://medium.com/@sonots
zozotech
70億人のファッションを技術の力で変えていく
https://tech.zozo.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away