Class: Server::Conf

Inherits:
Object
  • Object
show all
Defined in:
lib/server/conf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Conf

Returns a new instance of Conf.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/server/conf.rb', line 14

def initialize(opts)
  configure! if ARGV.include?('--delete-config') || !paths[:config].exist?

  # Check whether we're able to properly interact with the config
  @okay = true
  @okay = false if !paths[:sys_dir].writable?
  @okay = false if !paths[:app_dir].writable?
  @okay = false if !paths[:config].writable?

  @okay ? $Log.info("Environment ready to configure") : $Log.error("Unable to configure environment")

  check_permissions

  # Now create necessary files
  create! if !paths[:config].exist?
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/server/conf.rb', line 12

def config
  @config
end

#okayObject (readonly)

Returns the value of attribute okay.



11
12
13
# File 'lib/server/conf.rb', line 11

def okay
  @okay
end

Instance Method Details

#check_permissionsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/server/conf.rb', line 39

def check_permissions
  $Log.debug("Checking permissions for config module")
  rows = []
  paths.each do |name, path|
    good = "".green
    bad = "".red
    exists = path.exist? ? good : bad
    readable = path.readable? ? good : bad
    writable = path.writable? ? good : bad

    rows << [name, path, exists, readable, writable]

    # msg = "#{name} (#{path}): "
    # path.exist? ? msg = "#{msg}#{'exists ✓'.green}" : msg = "#{msg}#{'exists ✖'.red}"
    # path.readable? ? msg = "#{msg}#{', readable ✓'.green}" : msg = "#{msg}#{', readable ✖'.red}"
    # path.writable? ? msg = "#{msg}#{', writable ✓'.green}" : msg = "#{msg}#{', writable ✖'.red}"
    # $Log.info(msg)
  end

  table = Terminal::Table.new :title => "File Permissions", :headings => ['name', 'path', 'exists?', 'readable?', 'writable?'], :alignment => :center, :rows => rows
  puts table
end

#configure!Object



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
# File 'lib/server/conf.rb', line 62

def configure!
  delete! if ARGV.include?('--delete-config')

  $Log.info("Configuring file/folder paths")

  Step.start("Making sys_dir writable")
  FileUtils.chmod("u=wrx,go=rx", paths[:sys_dir]) if !paths[:sys_dir].exist?
  Step.complete()

  Step.start("Creating app_dir")
  FileUtils.mkdir(paths[:app_dir], :mode => 0755) if !paths[:app_dir].exist?
  Step.complete()

  Step.start("Making app_dir writable")
  FileUtils.chmod("u=wrx,go=rx", paths[:app_dir]) if !paths[:app_dir].writable?
  Step.complete()

  # Now let's see if the config file is blank. If so, it's the first
  # run and we need to create and store it.
  $Log.debug("Creating base JSON config if need be")
  create! if !read

  # $Log.debug("Config file: #{read}")
  # $Log.debug("Raw config file: #{File.read(paths[:config])}")

  return true
end

#create!Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/server/conf.rb', line 128

def create!
  $Log.info("Creating server-gem config file")
  config = {}

  [:packages, :sys, :installed, :conf, :history, :info, :log, :signals, :vim, :ssh, :cron, :build].each do |key|
    config[key] = {}
  end

  write!(config)
end

#delete!Object



139
140
141
142
143
144
# File 'lib/server/conf.rb', line 139

def delete!
  Step.start("Deleting existing config: #{paths[:config]}")
  FileUtils.rm(paths[:config]) if paths[:config].exist?

  paths[:config].exist? ? Step.fail("Failed to delete config file") : Step.complete
end

#get(item) ⇒ Object



120
121
122
# File 'lib/server/conf.rb', line 120

def get(item)
  #
end

#pathsObject



31
32
33
34
35
36
37
# File 'lib/server/conf.rb', line 31

def paths
  {
    :config => Pathname.new(Server.config_path),
    :sys_dir => Pathname.new(Server.sys_dir),
    :app_dir => Pathname.new(Server.app_dir)
  }
end

#readObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/server/conf.rb', line 90

def read

  $Log.debug("Parsing config file: #{paths[:config]}")
  config = begin
    if paths[:config].exist? && File.size?(paths[:config])
      Logger.debug("File exists and has size: #{File.size?(paths[:config])}")
      json = File.read(paths[:config])
      JSON.parse(json) if json != ""
    else
      $Log.fatal("Can't read config.json... Did you break something?")
      return false
    end
  rescue JSON::ParserError, TypeError => e
    $Log.error("Could not parse config file: #{e.message}")
    $Log.error(e.backtrace)
  end
end

#update(opts) ⇒ Object



124
125
126
# File 'lib/server/conf.rb', line 124

def update(opts)
  #
end

#write!(config) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/server/conf.rb', line 108

def write!(config)
  # $Log.debug("Writing config: #{config}")
  if !config.respond_to?("to_json")
    $Log.error("Unable to convert configuration to JSON. Is the gem installed?")
    return false
  else
    File.open(paths[:config], "w") do |f|
      f.write(config.to_json)
    end
  end
end