Initial commit
Dev

Dev commited on 2018-06-14 23:07:57
Showing 9 changed files, with 218 additions and 0 deletions.

... ...
@@ -0,0 +1,9 @@
1
+# IntelliJ IDEA files.
2
+.idea
3
+*.iml
4
+
5
+# Holding back some non public info
6
+TASK.md
7
+
8
+# OS files
9
+.directory
... ...
@@ -0,0 +1,4 @@
1
+#!/usr/bin/env bash
2
+
3
+which ruby
4
+test "$?" -ne 0 && sudo apt install ruby2.0
0 5
\ No newline at end of file
... ...
@@ -0,0 +1,18 @@
1
+- packages:
2
+  - name: apache2
3
+    version: 2.4.7-1ubuntu4.20
4
+    desired_state: installed
5
+  - name: php5
6
+    version: 5.5.9+dfsg-1ubuntu4.25
7
+    desired_state: installed
8
+  - name: libapache2-mod-php5
9
+    version: 5.5.9+dfsg-1ubuntu4.25
10
+    desired_state: installed
11
+
12
+- files:
13
+  - path: /tmp/hello_world.php
14
+    owner: apache2
15
+    group: apache2
16
+    mode: 0644
17
+    local_file: hello_world.php
18
+    desired_state: present
... ...
@@ -0,0 +1,3 @@
1
+<?php
2
+    header("Content-Type: text/plain");
3
+    echo "Hello, world!\n";
... ...
@@ -0,0 +1,43 @@
1
+module RCM
2
+  class File
3
+    attr_accessor :path, :owner, :group, :mode, :local_content_md5, :target_content_md5, :current_state, :desired_state
4
+
5
+    def ==(other)
6
+      return false unless other.is_a?(RCM::File)
7
+
8
+      @path == other.path &&
9
+          @owner == other.owner &&
10
+          @group == other.group &&
11
+          @mode == other.mode &&
12
+          @content_md5 == other.content_md5 &&
13
+          @desired_state == other.desired_state &&
14
+          @current_state == other.current_state
15
+    end
16
+
17
+    def initialize(path, owner, group, mode, local_content_md5, target_content_md5, current_state, desired_state)
18
+      @path = path
19
+      @owner = owner
20
+      @group = group
21
+      @mode = mode
22
+      @local_content_md5 = local_content_md5
23
+      @target_content_md5 = target_content_md5
24
+      @current_state = current_state
25
+      @desired_state = desired_state
26
+    end
27
+
28
+    def lackin(other)
29
+      raise 'not implemented.'
30
+    end
31
+
32
+    def to_s
33
+      "Path = #{@path}\n" +
34
+      "Owner = #{@owner}\n" +
35
+      "Group = #{@group}\n" +
36
+      "Mode = #{@mode}\n" +
37
+      "Local Content MD5 = #{@local_content_md5}\n" +
38
+      "Target Content MD5 = #{@target_content_md5}\n" +
39
+      "Desired State = #{@desired_state}\n" +
40
+      "Current State = #{@current_state}"
41
+    end
42
+  end
43
+end
... ...
@@ -0,0 +1,29 @@
1
+module RCM
2
+  class Package
3
+    attr_accessor :name, :version, :current_state, :desired_state
4
+
5
+    def ==(other)
6
+      return false unless other.is_a?(RCM::Package)
7
+
8
+      @installed = other.installed &&
9
+      @name == other.name &&
10
+      @version == other.version &&
11
+      @current_state == other.current_state &&
12
+      @desired_state == other.desired_state
13
+    end
14
+
15
+    def initialize(name, version, current_state, desired_state)
16
+      @name = name
17
+      @version = version
18
+      @current_state = current_state
19
+      @desired_state = desired_state
20
+    end
21
+
22
+    def to_s
23
+      "Name = #{@name}\n" +
24
+      "Version = #{@version}\n" +
25
+      "Current State = #{@current_state}\n" +
26
+      "Desired State = #{@desired_state}"
27
+    end
28
+  end
29
+end
... ...
@@ -0,0 +1,28 @@
1
+module RCM
2
+  class Service
3
+    attr_accessor :name, :installed, :definition_path, :running, :enabled
4
+
5
+    def ==(other)
6
+      return false unless other.is_a?(RCM::Service)
7
+
8
+      @name == other.name && @installed == other.installed && @definition_path == other.definition_path &&
9
+          @running == other.running && @enabled == other.enabled
10
+    end
11
+
12
+    def initialize(name, installed, definition_path, running, enabled)
13
+      @name = name
14
+      @installed = installed
15
+      @definition_path = definition_path
16
+      @running = running
17
+      # Enabled to run on startup (SystemD-esque definition)
18
+      @enabled = enabled
19
+    end
20
+
21
+    def to_s
22
+      "Name = #{@name}\n" +
23
+          "Installed = #{@installed}\n" +
24
+          "Definition Path = #{@definition_path}\n" +
25
+          "Running = #{@running}\nStart at boot = #{@enabled}"
26
+    end
27
+  end
28
+end
... ...
@@ -0,0 +1,21 @@
1
+require 'open3'
2
+
3
+module RCM
4
+  module_function :cmd
5
+
6
+  def cmd(command)
7
+    stdin, stdout, stderr, wait_thr = Open3.popen3(command)
8
+    output = stdout.gets(nil)
9
+    stdout.close
10
+    errors = stderr.gets(nil)
11
+    stderr.close
12
+    exit_code = Integer(wait_thr.value)
13
+
14
+    {
15
+      exit_code: exit_code,
16
+      output: output,
17
+      errors: errors
18
+    }
19
+
20
+  end
21
+end
... ...
@@ -0,0 +1,63 @@
1
+require_relative 'rcm_file'
2
+require_relative 'rcm_package'
3
+require_relative 'rcm_service'
4
+require 'yaml'
5
+
6
+module RCM
7
+  PACKAGES = 'packages'
8
+  FILES = 'files'
9
+  SERVICES = 'services'
10
+
11
+  @@wanted = {
12
+    PACKAGES => [],
13
+    FILES => [],
14
+    SERVICES => []
15
+  }
16
+
17
+  @@got = {
18
+    PACKAGES => [],
19
+    FILES => [],
20
+    SERVICES => []
21
+  }
22
+
23
+  def converge(old_state, new_state)
24
+    if old_state.is_a?(RCM::File)
25
+    end
26
+
27
+  end
28
+
29
+  def whachuwant()
30
+    # Consume config from YAML, and convert it to usable objects in @@wanted.
31
+    raise 'config.yaml not found.' unless ::File.file?('config.yaml')
32
+
33
+    config = YAML.load_file('config.yaml')
34
+    config.each do |yaml_objects|
35
+      yaml_objects.each do |coll, defs|
36
+        case coll
37
+        when PACKAGES
38
+          defs.each do |d|
39
+            p = RCM::Package.new(d['name'], d['version'], 'idk', d['desired_state'])
40
+            @@wanted[PACKAGES].push(p)
41
+          end
42
+
43
+        when FILES
44
+          defs.each do |d|
45
+            f = RCM::File.new(d['path'], d['owner'], d['group'], d['mode'], 'idk',
46
+                              'idk', 'idk', d[''])
47
+            @@wanted[FILES].push(f)
48
+          end
49
+        end
50
+      end
51
+    end
52
+
53
+  end
54
+
55
+  def whachugot()
56
+
57
+  end
58
+
59
+  module_function :whachuwant, :whachugot
60
+
61
+end
62
+
63
+RCM.whachuwant
0 64