Class: Cindy::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/cindy/template.rb

Constant Summary collapse

IDENT_STRING =
' ' * 4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, file, name) ⇒ Template

Returns a new instance of Template.



10
11
12
13
14
15
16
17
18
# File 'lib/cindy/template.rb', line 10

def initialize(owner, file, name)
    @owner = owner # owner is Cindy objet
    @file = file # local template filename
    @alias = name
    @paths = {}   # remote filenames (<environment name> => <filename>)
    @defvars = {} # default/global variables
    @envvars = {} # environment specific variables
    @postcmds = [] # commands to run after deployment
end

Instance Attribute Details

#aliasObject (readonly)

Returns the value of attribute alias.



8
9
10
# File 'lib/cindy/template.rb', line 8

def alias
  @alias
end

#defvarsObject (readonly)

Returns the value of attribute defvars.



8
9
10
# File 'lib/cindy/template.rb', line 8

def defvars
  @defvars
end

#envvarsObject (readonly)

Returns the value of attribute envvars.



8
9
10
# File 'lib/cindy/template.rb', line 8

def envvars
  @envvars
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/cindy/template.rb', line 8

def file
  @file
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/cindy/template.rb', line 8

def paths
  @paths
end

#postcmdsObject (readonly)

Returns the value of attribute postcmds.



8
9
10
# File 'lib/cindy/template.rb', line 8

def postcmds
  @postcmds
end

Instance Method Details

#add_postcmd(cmd, options) ⇒ Object



43
44
45
# File 'lib/cindy/template.rb', line 43

def add_postcmd(cmd, options)
    @postcmds << cmd
end

#deploy(env) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cindy/template.rb', line 47

def deploy(env)
    executor = executor_for_env env
    remote_filename = @paths[env.name]
    sudo = ''
    sudo = 'sudo' unless 0 == executor.exec('id -u').to_i
    suffix = executor.exec('date \'+%Y%m%d%H%M\'') # use remote - not local - time machine
    executor.exec("[ -e \"#{remote_filename}\" ] && [ ! -h \"#{remote_filename}\" ] && #{sudo} mv -i \"#{remote_filename}\" \"#{remote_filename}.pre\"")
    executor.exec("#{sudo} tee #{remote_filename}.#{suffix} > /dev/null", stdin: render(env, executor))
    executor.exec("#{sudo} ln -snf \"#{remote_filename}.#{suffix}\" \"#{remote_filename}\"")
    shell = executor.exec('ps -p $$ -ocomm=')
    env = { 'INSTALL_FILE' => remote_filename }
    env_string = env.inject([]) { |a, b| a << b.map(&:shellescape).join('=') }.join(' ')
    @postcmds.each do |cmd|
        executor.exec("#{sudo} #{'env' if shell =~ /csh\z/} #{env_string} sh -c '#{cmd}'") # TODO: escape single quotes in cmd?
    end
    executor.close
end

#list_variables(envname) ⇒ Object

def variables

    (@defvars.keys + @envvars.collect { |v| v[1].keys }.flatten).uniq
end


69
70
71
72
73
# File 'lib/cindy/template.rb', line 69

def list_variables(envname)
    @defvars.merge(@envvars[envname]).each_pair do |k,v|
        puts "- #{k}#{' (default)' unless @envvars[envname].key? k } = #{v} (#{v.class.name})"
    end
end


39
40
41
# File 'lib/cindy/template.rb', line 39

def print(env)
    puts render(env)
end

#set_path_for_environment(envname, path) ⇒ Object



100
101
102
103
104
# File 'lib/cindy/template.rb', line 100

def set_path_for_environment(envname, path)
    envname = envname.intern
    @paths[envname] = path
    @envvars[envname] ||= {}
end

#set_variable(envname, varname, value) ⇒ Object

def rename_variable(oldvarname, newvarname)

    @defvars[newvarname] = value if value = @defvars.delete(oldvarname)
    @envvars.each_value do |h|
        h[newvarname] = value if value = h.delete(oldvarname)
    end
end


89
90
91
92
93
94
95
96
97
98
# File 'lib/cindy/template.rb', line 89

def set_variable(envname, varname, value)
    envname = envname.intern if envname
    varname = varname.intern
    STDERR.puts "[ WARN ] non standard variable name found" unless varname =~ /\A[a-z][a-z0-9_]*\z/
    if envname
        @envvars[envname][varname] = value
    else
        @defvars[varname] = value
    end
end

#to_sObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cindy/template.rb', line 22

def to_s
    ret = ["template :#{@alias}, #{@file.inspect} do"]
    @defvars.each_pair do |k,v|
        ret << "#{IDENT_STRING * 1}var :#{k}, #{v.inspect}"
    end
    @paths.each_pair do |ke,ve|
       ret << "#{IDENT_STRING * 1}on :#{ke}, #{ve.inspect} do"
       @envvars[ke].each_pair do |kv,vv|
           ret << "#{IDENT_STRING * 2}var :#{kv}, #{vv.inspect}"
       end
       ret << "#{IDENT_STRING * 1}end"
    end
    ret << "end"
    ret << ''
    ret.join "\n"
end