Last active
December 12, 2021 09:22
-
-
Save miladoll/1908553174aad353f072ca28695ce252 to your computer and use it in GitHub Desktop.
Itamaeでなんかいちどだけexecuteを実行するための適当なプラグイン
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fileutils' | |
require 'digest/md5' | |
require "itamae/resource/base" | |
require "itamae/logger" | |
module Itamae | |
module Plugin | |
module Resource | |
class ExecuteOnce < Itamae::Resource::Base | |
define_attribute :action, default: :execute_once | |
define_attribute :cmd, type: String, default_name: true | |
def action_execute_once( options ) | |
history_done = '/var/itamae/done/execute_once/' | |
test_remote = run_command( "test -d #{history_done}", error: false ) | |
if test_remote.exit_status != 0 | |
run_command( "mkdir -p #{history_done}" ) | |
end | |
cmd_line = attributes.cmd | |
hashed = Digest::MD5.hexdigest( cmd_line ) | |
hashed_dir = history_done + hashed | |
test_remote = run_command( "test -d #{hashed_dir}", error: false ) | |
if test_remote.exit_status == 0 | |
Itamae.logger.info "execute_once SKIP[#{cmd_line}]" | |
return | |
end | |
result = run_command( cmd_line, error: false ) | |
if result.exit_status != 0 | |
raise "execute_once caused error: #{run_command}" | |
else | |
run_command( "mkdir -p #{hashed_dir}", error: false ) | |
Itamae.logger.info "execute_once done[#{cmd_line}]" | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment