Class: VagrantPlugins::Tagprovision::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-tagprovision/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Action

Returns a new instance of Action.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-tagprovision/action.rb', line 7

def initialize(app,env)
  @app     = app
  @ui      = env[:ui]
  @machine = env[:machine].name.to_s
  @machinfo = env[:machine]
  @commit_hash = `git rev-parse HEAD`
  @commit_hash.strip!
  @current_tag = `git symbolic-ref -q HEAD || git describe --tags --exact-match`
  @time = Time.new
  @tag = "provisioned_#{@machine}_#{@time.year}#{@time.month}#{@time.day}#{@time.hour}#{@time.min}#{@time.sec}"
  @machine_env = @machinfo.env;
  @tmp_path = @machine_env.tmp_path;
  @config = Util.get_config(@machine_env)
  
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-tagprovision/action.rb', line 23

def call(env)
  # Before machine action
  state  = env[:machine].state.id

  # Execute machine action
  @app.call(env)

  # After execute machine action
  action    = env[:machine_action]
  provision = env[:provision_enabled]

  case action
    when :up
      #env[:ui].info("Vagrant Hook Up:  #{@machine}")
    when :reload
      #env[:ui].info("Vagrant Hook Reload:  #{@machine}")
    
    when :provision
      if(@config.tagprovision.enabled)
        if(@config.tagprovision.log_provision)
          log_provision(env)
        end
        if(@config.tagprovision.tag_provision)
          tag_provision(env)
        end
      end
  end
end

#log_provision(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant-tagprovision/action.rb', line 60

def log_provision(env) 
  env[:ui].info("Logging provisioning information to guest machine:  #{@machine}")

  if (@machinfo.communicate.test("uname -s | grep SunOS"))
    reallogfile = '/var/log/provision_log'
    move_cmd = 'mv'
  elsif (@machinfo.communicate.test("test -d $Env:SystemRoot"))
    windir = ""
    @machinfo.communicate.execute("echo %SYSTEMROOT%", {:shell => :cmd}) do |type, contents|
      windir << contents.gsub("\r\n", '') if type == :stdout
    end
    reallogfile = "#{windir}\\System32\\drivers\\var\\provision_log"
    move_cmd = 'mv -force'
  else
    reallogfile = '/var/log/provision_log'
    move_cmd = 'mv -f'
  end

  file = @tmp_path.join("hosts.#{@machinfo.name}_#{@commit_hash}")

  # Make sure the log files exist and are writable.
  @machinfo.communicate.sudo("touch #{reallogfile}")
  @machinfo.communicate.sudo("chmod 666 #{reallogfile}")
  @machinfo.communicate.sudo("touch /tmp/provision_logs")
  @machinfo.communicate.sudo("chmod 666 /tmp/provision_logs")

  # download the current log file
  @machinfo.communicate.download(reallogfile, file)
  
  if update_file(file, @machinfo, false)
    # upload modified file and remove temporary file
    @machinfo.communicate.upload(file, '/tmp/provision_logs')
    if windir
      @machinfo.communicate.sudo("#{move_cmd} /tmp/provision_logs/hosts.#{@machinfo.name} #{reallogfile}")
    elsif @machinfo.communicate.test('test -f /.dockerinit')
      @machinfo.communicate.sudo("cat /tmp/provision_logs > #{reallogfile}")
    else
      @machinfo.communicate.sudo("#{move_cmd} /tmp/provision_logs #{reallogfile}")
    end
  end            
end

#tag_provision(env) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/vagrant-tagprovision/action.rb', line 52

def tag_provision(env)
  env[:ui].info("Tagging provisioning information:  #{@machine}")
  `git tag #{@tag}`
  if(@config.tagprovision.push_tag)
    `git push --tags`
  end
end

#update_file(file, resolving_machine = nil, include_id = true) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-tagprovision/action.rb', line 102

def update_file(file, resolving_machine = nil, include_id = true)
  #update the file with the current state of the vagrant config and the user provisioning
  file = Pathname.new(file)
  file_content_line = "#{@time.inspect}\tProvisioned\t#{@commit_hash}\t#{@current_tag}"
  old_file_content = file.read
  new_file_content = old_file_content + "#{file_content_line}"
  file.open('wb') { |io| io.write(new_file_content) }
  old_file_content != new_file_content
end