Class: EPM::Deploy

Inherits:
Object
  • Object
show all
Defined in:
lib/epm/deploy.rb

Instance Method Summary collapse

Constructor Details

#initialize(def_file, settings = {}) ⇒ Deploy

Returns a new instance of Deploy.



5
6
7
8
9
10
11
# File 'lib/epm/deploy.rb', line 5

def initialize def_file, settings={}
  @dirname  = File.dirname(File.absolute_path(def_file))
  @log_file = File.join(ENV['HOME'], '.epm', 'deployed-log.csv')
  setup settings
  @brain    = {}
  @def_file = def_file
end

Instance Method Details

#deploy(command) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/epm/deploy.rb', line 49

def deploy command
  deploy_k = command.shift
  k_name = command.shift
  p "Deploying #{deploy_k} with name of #{k_name.gsub(/(\{|\})/,'')}."
  k_address = EPM::Create.new(File.join(@dirname, deploy_k), @settings).deploy
  @brain[k_name] = k_address
  p "#{k_name} => #{k_address}"
end

#deploy_packageObject



13
14
15
16
17
18
19
20
21
22
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
# File 'lib/epm/deploy.rb', line 13

def deploy_package
  get_remote_if_remote
  if @def_file
    p "Deploying: #{@def_file}."
    commands = File.readlines @def_file
    commands = commands.reject{|cmd| cmd[/\A#/] || cmd[/\A$/]}.map{|cmd| cmd.gsub("\n",'')}
    commands = commands.inject([]) do |arr,ele|
      ele[/\A\S+/] ? arr << [ele] : arr[-1] << ele.strip.split(' => ')
      arr
    end
    commands.each do |cmd|
      dowit = cmd.shift
      case dowit
      when 'deploy:'
        deploy cmd.shift
        sleep 1
      when 'modify-deploy:'
        modify_deploy cmd
        sleep 1
      when 'transact:'
        transact cmd.shift
        sleep 1
      when 'query:'
        query cmd.shift
      when 'log:'
        log cmd.shift
      when 'set:'
        set_var cmd.shift
      when 'endow:'
        endow cmd.shift
        sleep 1
      end
    end
  end
end

#endow(command) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/epm/deploy.rb', line 94

def endow command
  recipient = command.shift
  until ! recipient[/(\{\{.*?\}\})/]
    recipient.gsub!($1, @brain[$1])
  end
  endowment = command.shift
  p "Sending #{recipient} an endowment of: #{endowment}."
  EPM::Transact.new(recipient, [''], @settings).transact endowment
end

#get_remote_if_remoteObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/epm/deploy.rb', line 141

def get_remote_if_remote
  if is_it_a_remote_file?
    begin
      p "Cloning into Remote."
      tmp = Tempfile.new 'epm'
      Dir.chdir File.dirname tmp.path
      unless File.directory? 'cloned'
        Dir.mkdir 'cloned'
      end
      Dir.chdir 'cloned'
      begin
        `git clone #{@def_file}`
      end
      dir = Dir.glob('*')[0]
      dir = File.dirname(tmp.path) + '/cloned/' + dir
      Dir.chdir dir
      def_files = Dir.glob('*').select {|f| File.extname(f) == '.package-definition'}
      if def_files.empty?
        dirs = Dir.glob('*').select {|f| File.directory? f}
        dirs.each do |d|
          Dir.chdir d
          def_files << Dir.glob('*').inject([]) do |arr,f|
            if File.extname(f) == '.package-definition'
              arr << d + '/' + f
            end
            arr
          end
          Dir.chdir dir
        end
      end
      def_files.flatten!
      def_files.each do |df|
        this_def_dir = File.dirname df
        this_def_file = File.basename df
        Dir.chdir this_def_dir
        EPM::Deploy.new(this_def_file, @settings).deploy_package
        Dir.chdir dir
      end
    rescue Exception => e
      p "There was an error during that deploy."
      puts e.message
    ensure
      remote_cleanup tmp
      tmp.unlink
      @def_file = false
    end
  end
end

#is_it_a_remote_file?Boolean

Returns:

  • (Boolean)


190
191
192
193
194
195
196
197
# File 'lib/epm/deploy.rb', line 190

def is_it_a_remote_file?
  @remote = false
  if @def_file[/https*:\/\//] || @def_file[/gits*:\/\//] || @def_file[/.+@.+\..+:/]
    @remote = true
    return true
  end
  false
end

#log(command) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/epm/deploy.rb', line 120

def log command
  address = command.shift
  name = command.shift
  until ! address[/(\{\{.*?\}\})/]
    address.gsub!($1,@brain[$1])
  end
  until ! name[/(\{\{.*?\}\})/]
    name.gsub!($1,@brain[$1])
  end
  log_entry = [address, name].join(',')
  p "Logging [#{address},#{name}]"
  `echo #{log_entry} >> #{@log_file}`
end

#modify_deploy(command) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/epm/deploy.rb', line 58

def modify_deploy command
  deploy_k = command[0].shift
  deploy_k = EPM::FileHelpers.file_guard(File.join(@dirname, deploy_k))
  k_name = command[0].shift
  dump = command.shift
  until command.empty?
    cmd = command.shift
    to_replace = cmd.shift
    replacer = cmd.shift
    until ! replacer[/(\{\{.*?\}\})/]
      replacer.gsub!($1, @brain[$1])
    end
    k_value = File.read deploy_k
    k_value = k_value.gsub "#{to_replace}", "#{replacer}"
    File.open(deploy_k, 'w'){|f| f.write k_value}
  end
  p "After modifying, am deploying #{deploy_k} with name of #{k_name.gsub(/(\{|\})/,'')}."
  k_address = EPM::Create.new(deploy_k, @settings).deploy
  @brain[k_name] = k_address
  p "#{k_name} => #{k_address}"
end

#query(command) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/epm/deploy.rb', line 104

def query command
  contract = command.shift
  address = command.shift
  name = command.shift
  until ! contract[/(\{\{.*?\}\})/]
    contract.gsub!($1,@brain[$1])
  end
  until ! address[/(\{\{.*?\}\})/]
    address.gsub!($1,@brain[$1])
  end
  p "Querying #{contract} at: #{address}."
  storage = EPM::Query.new(contract, address, @settings).query
  p "#{contract}:#{address} => #{storage}"
  @brain[name] = storage
end

#remote_cleanup(tmp) ⇒ Object



199
200
201
202
203
204
# File 'lib/epm/deploy.rb', line 199

def remote_cleanup tmp
  if @remote
    Dir.chdir File.dirname tmp.path
    FileUtils.rm_rf 'cloned'
  end
end

#set_var(command) ⇒ Object



134
135
136
137
138
139
# File 'lib/epm/deploy.rb', line 134

def set_var command
  key = command.shift
  value = command.shift
  p "Setting #{key} to #{value}"
  @brain[key] = value
end

#setup(settings) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/epm/deploy.rb', line 206

def setup settings
  unless settings.empty?
    @settings = settings
  else
    @settings = EPM::Settings.check
  end
end

#transact(command) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/epm/deploy.rb', line 80

def transact command
  recipient = command.shift
  until ! recipient[/(\{\{.*?\}\})/]
    recipient.gsub!($1, @brain[$1])
  end
  data = command.shift
  until ! data[/(\{\{.*?\}\})/]
    data.gsub!($1,@brain[$1])
  end
  data = data.split(' ')
  p "Sending #{recipient} the data: #{data}."
  EPM::Transact.new(recipient, data, @settings).transact
end