codestrap 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +4 -0
- data/.yardopts +3 -0
- data/BOILERPLATE.md +151 -0
- data/CODESTRAPFILE.md +28 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +13 -0
- data/README.md +69 -0
- data/Rakefile +51 -0
- data/bin/strap +8 -0
- data/bin/upstrap +8 -0
- data/codestrap.gemspec +37 -0
- data/config/projects/codestrap.rb +22 -0
- data/config/software/codestrap.rb +20 -0
- data/config/software/ruby.rb +247 -0
- data/config/software/zlib.rb +86 -0
- data/features/client.feature +99 -0
- data/features/fixtures/client/home/codestrap/bin/.empty +0 -0
- data/features/fixtures/client/home/codestrap/content/bash.erb +3 -0
- data/features/fixtures/client/home/codestrap/content/gem/dir/file +0 -0
- data/features/fixtures/client/home/codestrap/content/gem/dir/template +3 -0
- data/features/fixtures/client/home/codestrap/content/gem/file +1 -0
- data/features/fixtures/client/home/codestrap/content/gem/template +3 -0
- data/features/fixtures/client/home/codestrap/content/local_remote.erb +1 -0
- data/features/fixtures/client/home/codestrap/content/local_remote/dir/file +0 -0
- data/features/fixtures/client/home/codestrap/content/local_remote/dir/template +3 -0
- data/features/fixtures/client/home/codestrap/content/local_remote/file +1 -0
- data/features/fixtures/client/home/codestrap/content/local_remote/template +3 -0
- data/features/fixtures/client/home/codestrap/objects/local.json +3 -0
- data/features/fixtures/client/output/.empty +0 -0
- data/features/fixtures/client/var/lib/codestrap/bin/.empty +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/bashremote.erb +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/gemremote/dir/file +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/gemremote/dir/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/gemremote/file +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/gemremote/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_local.erb +1 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_local/dir/file +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_local/dir/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_local/file +1 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_local/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_remote.erb +1 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_remote/dir/file +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_remote/dir/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_remote/file +0 -0
- data/features/fixtures/client/var/lib/codestrap/content/remote_remote/template +3 -0
- data/features/fixtures/client/var/lib/codestrap/objects/remote.json +3 -0
- data/features/fixtures/logging/home/logging/codestrap/bin/stubdirectory/.empty +0 -0
- data/features/fixtures/logging/home/logging/codestrap/bin/stubnotlink +0 -0
- data/features/fixtures/logging/home/logging/codestrap/content/.empty +0 -0
- data/features/fixtures/logging/home/logging/codestrap/objects/.empty +0 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/address.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/cell.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/email.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/mobile.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/name.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/phone.erb +3 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/puppetmodule/Modulefile +12 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/puppetmodule/README +17 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/puppetmodule/manifests/init.pp +42 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/puppetmodule/spec/spec_helper.rb +18 -0
- data/features/fixtures/server/mixed/var/lib/stub/content/puppetmodule/tests/init.pp +13 -0
- data/features/fixtures/server/mixed/var/lib/stub/objects/contact.json +8 -0
- data/features/fixtures/server/mixed/var/lib/stub/objects/shadow/contact.json +8 -0
- data/features/fixtures/strap/projects/etc/.empty +0 -0
- data/features/fixtures/strap/projects/home/user/dir/bin/.empty +0 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/Modulefile +12 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/README +17 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/manifests/:stub:project.name:/subclass.pp +3 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/manifests/init.pp +42 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/spec/spec_helper.rb +18 -0
- data/features/fixtures/strap/projects/home/user/dir/content/puppetmodule/tests/init.pp +13 -0
- data/features/fixtures/strap/projects/home/user/dir/objects/contact.json +8 -0
- data/features/fixtures/strap/projects/home/user/dir/objects/shadow/contact.json +8 -0
- data/features/fixtures/strap/projects/output/.empty +0 -0
- data/features/fixtures/stub/contact/etc/.empty +0 -0
- data/features/fixtures/stub/contact/home/user/dir/bin/.empty +0 -0
- data/features/fixtures/stub/contact/home/user/dir/content/address.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/content/cell.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/content/email.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/content/mobile.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/content/name.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/content/phone.erb +3 -0
- data/features/fixtures/stub/contact/home/user/dir/objects/contact.json +8 -0
- data/features/fixtures/stub/contact/home/user/dir/objects/shadow/contact.json +8 -0
- data/features/fixtures/stub/contact/output/.empty +0 -0
- data/features/fixtures/stub/date/etc/.empty +0 -0
- data/features/fixtures/stub/date/home/user/stub/bin/.empty +0 -0
- data/features/fixtures/stub/date/home/user/stub/content/day.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/content/hour.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/content/minute.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/content/month.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/content/second.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/content/year.erb +3 -0
- data/features/fixtures/stub/date/home/user/stub/objects/.empty +0 -0
- data/features/fixtures/stub/date/output/.empty +0 -0
- data/features/fixtures/stub/script/etc/.empty +0 -0
- data/features/fixtures/stub/script/home/user/stub/bin/.empty +0 -0
- data/features/fixtures/stub/script/home/user/stub/content/credentials.erb +6 -0
- data/features/fixtures/stub/script/home/user/stub/objects/credentials.rb +11 -0
- data/features/fixtures/stub/script/output/.empty +0 -0
- data/features/logging.feature +39 -0
- data/features/object_scripts.feature +13 -0
- data/features/server.feature +36 -0
- data/features/step_definitions/client_steps.rb +99 -0
- data/features/step_definitions/logging_steps.rb +65 -0
- data/features/step_definitions/object_scripts_steps.rb +26 -0
- data/features/step_definitions/server_steps.rb +112 -0
- data/features/step_definitions/strap_steps.rb +69 -0
- data/features/step_definitions/stub_option_simple_steps.rb +31 -0
- data/features/step_definitions/stub_steps.rb +94 -0
- data/features/strap.feature +34 -0
- data/features/stub.feature +157 -0
- data/features/stub_option_simple.feature +14 -0
- data/features/stub_packages.feature +15 -0
- data/features/support/env.rb +197 -0
- data/lib/codestrap.rb +415 -0
- data/lib/codestrap/cli.rb +76 -0
- data/lib/codestrap/client.rb +179 -0
- data/lib/codestrap/config.rb +199 -0
- data/lib/codestrap/log.rb +56 -0
- data/lib/codestrap/mixin.rb +20 -0
- data/lib/codestrap/namespace.rb +19 -0
- data/lib/codestrap/object/abstract.rb +33 -0
- data/lib/codestrap/object/factory.rb +86 -0
- data/lib/codestrap/object/standard/datetime.rb +24 -0
- data/lib/codestrap/object/standard/files.rb +40 -0
- data/lib/codestrap/object/standard/project.rb +33 -0
- data/lib/codestrap/object/standard/rest.rb +30 -0
- data/lib/codestrap/patch.rb +122 -0
- data/lib/codestrap/server/rest.rb +177 -0
- data/lib/codestrap/server/version.rb +5 -0
- data/lib/codestrap/strap/abstract.rb +135 -0
- data/lib/codestrap/strap/factory.rb +85 -0
- data/lib/codestrap/strap/standard.rb +95 -0
- data/lib/codestrap/stub/abstract.rb +156 -0
- data/lib/codestrap/stub/factory.rb +87 -0
- data/lib/codestrap/stub/standard.rb +23 -0
- data/lib/codestrap/version.rb +3 -0
- data/omnibus.rb +53 -0
- data/package-scripts/codestrap/postinst +25 -0
- data/package-scripts/codestrap/postrm +17 -0
- data/package-scripts/codestrap/preinst +7 -0
- data/package-scripts/codestrap/prerm +15 -0
- data/resources/codestrap/pkg/background.png +0 -0
- data/resources/codestrap/pkg/distribution.xml.erb +22 -0
- data/resources/codestrap/pkg/license.html.erb +202 -0
- data/resources/codestrap/pkg/welcome.html.erb +7 -0
- metadata +466 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'ostruct'
|
|
2
|
+
|
|
3
|
+
module Codestrap
|
|
4
|
+
class Namespace
|
|
5
|
+
def initialize(hash)
|
|
6
|
+
hash.each do |key, value|
|
|
7
|
+
add_object key, value
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def get_binding
|
|
12
|
+
binding
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def add_object key, value
|
|
16
|
+
singleton_class.send(:define_method, key.to_s) { value }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'codestrap/object/abstract'
|
|
2
|
+
|
|
3
|
+
module Codestrap
|
|
4
|
+
module Object
|
|
5
|
+
# @abstract
|
|
6
|
+
# Objects used in templates
|
|
7
|
+
#
|
|
8
|
+
# Contains class methods used by object factory
|
|
9
|
+
class Abstract
|
|
10
|
+
class << self
|
|
11
|
+
# List of directories
|
|
12
|
+
# @!attribute [rw] files
|
|
13
|
+
# @return [Array]
|
|
14
|
+
attr_accessor :dirs
|
|
15
|
+
|
|
16
|
+
# CLI object
|
|
17
|
+
# @!attribute [rw] cli
|
|
18
|
+
# @return [Codestrap::CLI]
|
|
19
|
+
attr_accessor :cli
|
|
20
|
+
|
|
21
|
+
# Configuration object
|
|
22
|
+
# @!attribute [rw] config
|
|
23
|
+
# @return [Codestrap::Config]
|
|
24
|
+
attr_accessor :config
|
|
25
|
+
|
|
26
|
+
# REST clients
|
|
27
|
+
# @!attribute [rw] clients
|
|
28
|
+
# @return [Array<Codestrap::Clients>]
|
|
29
|
+
attr_accessor :clients
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require 'codestrap/patch'
|
|
2
|
+
require 'ostruct'
|
|
3
|
+
require 'yaml'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'codestrap/object/standard/datetime'
|
|
6
|
+
require 'codestrap/object/standard/files'
|
|
7
|
+
require 'codestrap/object/standard/project'
|
|
8
|
+
require 'codestrap/object/standard/rest'
|
|
9
|
+
module Codestrap
|
|
10
|
+
module Object
|
|
11
|
+
class Factory
|
|
12
|
+
def initialize(*args)
|
|
13
|
+
@namespace = args.shift.capitalize if args.length > 0
|
|
14
|
+
@namespace ||= 'Standard'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Directories where to find static object files
|
|
18
|
+
# @!attribute [rw] files
|
|
19
|
+
# @return [Codestrap::Config]
|
|
20
|
+
attr_accessor :dirs
|
|
21
|
+
|
|
22
|
+
# CLI object
|
|
23
|
+
# @!attribute [rw] cli
|
|
24
|
+
# @return [Codestrap::CLI]
|
|
25
|
+
attr_accessor :cli
|
|
26
|
+
|
|
27
|
+
# Configuration object
|
|
28
|
+
# @!attribute [rw] config
|
|
29
|
+
# @return [Codestrap::Config]
|
|
30
|
+
attr_accessor :config
|
|
31
|
+
|
|
32
|
+
# Array of client objects
|
|
33
|
+
# @!attribute [rw] clients
|
|
34
|
+
# @return [Array<Codestrap::Client>]
|
|
35
|
+
attr_accessor :clients
|
|
36
|
+
|
|
37
|
+
# Generate a hash of key, value pair
|
|
38
|
+
# If value is of type Hash it is converted into an [OpenStruct] object
|
|
39
|
+
#
|
|
40
|
+
# @return [Hash]
|
|
41
|
+
def objects
|
|
42
|
+
objects = {}
|
|
43
|
+
scan.each_pair do |key, value|
|
|
44
|
+
case value
|
|
45
|
+
when Hash
|
|
46
|
+
objects[key] = OpenStruct.new(value)
|
|
47
|
+
else
|
|
48
|
+
objects[key] = value
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
objects
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Scan for objects
|
|
55
|
+
#
|
|
56
|
+
# @return [Hash]
|
|
57
|
+
def scan
|
|
58
|
+
objects = {}
|
|
59
|
+
scan_class = "Codestrap::Object::#{@namespace.to_s}".split('::').inject(Object) { |o, c| o.const_get c }
|
|
60
|
+
klasses = scan_class.constants.map do |constant|
|
|
61
|
+
"Codestrap::Object::Standard::#{constant.to_s}".split('::').inject(Object) { |o, c| o.const_get c }
|
|
62
|
+
end
|
|
63
|
+
Array(klasses).sort { |a, b| a.weight <=> b.weight }.each do |klass|
|
|
64
|
+
klass.dirs = @dirs
|
|
65
|
+
klass.cli = @cli
|
|
66
|
+
klass.config = @config
|
|
67
|
+
klass.clients = @clients
|
|
68
|
+
klass.objects.each_pair do |key, value|
|
|
69
|
+
objects[key.downcase] = value
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
objects
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def to_hash
|
|
76
|
+
objects = {}
|
|
77
|
+
scan.each_pair do |key, value|
|
|
78
|
+
if value.is_a?(Hash)
|
|
79
|
+
objects[key] = value
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
objects
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'codestrap/object/abstract'
|
|
2
|
+
require 'date'
|
|
3
|
+
|
|
4
|
+
module Codestrap
|
|
5
|
+
module Object
|
|
6
|
+
module Standard
|
|
7
|
+
# Date and Time object directly from Rubys Datetime class
|
|
8
|
+
class Datetime < Codestrap::Object::Abstract
|
|
9
|
+
# Object(s) weight
|
|
10
|
+
# @return [Integer]
|
|
11
|
+
def self.weight
|
|
12
|
+
200
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Objects
|
|
16
|
+
# @return [Hash]
|
|
17
|
+
# @option
|
|
18
|
+
def self.objects
|
|
19
|
+
{ 'datetime' => DateTime.now }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require 'codestrap/object/abstract'
|
|
2
|
+
|
|
3
|
+
module Codestrap
|
|
4
|
+
module Object
|
|
5
|
+
module Standard
|
|
6
|
+
# Static file derived objects
|
|
7
|
+
#
|
|
8
|
+
# Create objects from .json, .yaml and executable files
|
|
9
|
+
class Files < Codestrap::Object::Abstract
|
|
10
|
+
class << self
|
|
11
|
+
# Object(s) weight
|
|
12
|
+
# @return [Integer]
|
|
13
|
+
def weight
|
|
14
|
+
100
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def objects
|
|
18
|
+
objects = {}
|
|
19
|
+
|
|
20
|
+
glob_array = Array(Files.dirs).map { |file| File.join(File.expand_path(file), '*') }
|
|
21
|
+
Codestrap::Patch::Dir.glob_files(glob_array).each do |file|
|
|
22
|
+
stat = File::Stat.new file
|
|
23
|
+
name = File.basename(file, File.extname(file)).downcase
|
|
24
|
+
case
|
|
25
|
+
when /\.json$/.match(file)
|
|
26
|
+
objects[name] = JSON.load(File.read(file))
|
|
27
|
+
when /\.yaml$/.match(file)
|
|
28
|
+
objects[name] = YAML.load(File.read(file))
|
|
29
|
+
when stat.executable?
|
|
30
|
+
objects[name] = JSON.parse(`#{file}`)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
objects
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'codestrap/object/abstract'
|
|
2
|
+
require 'ostruct'
|
|
3
|
+
|
|
4
|
+
module Codestrap
|
|
5
|
+
module Object
|
|
6
|
+
module Standard
|
|
7
|
+
# Project metadata derived object
|
|
8
|
+
class Project < Codestrap::Object::Abstract
|
|
9
|
+
class << self
|
|
10
|
+
# Object(s) weight
|
|
11
|
+
# @return [Integer]
|
|
12
|
+
def weight
|
|
13
|
+
50
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def objects
|
|
17
|
+
objects = {}
|
|
18
|
+
if Project.cli
|
|
19
|
+
project = OpenStruct.new()
|
|
20
|
+
mod = Project.cli.command
|
|
21
|
+
mod =~ /^(?:stub|strap)(\S+)$/
|
|
22
|
+
project.module = $1
|
|
23
|
+
project.name = File.basename(Project.cli.argv[0])
|
|
24
|
+
|
|
25
|
+
objects['project'] = project
|
|
26
|
+
end
|
|
27
|
+
objects
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'codestrap/object/abstract'
|
|
2
|
+
|
|
3
|
+
module Codestrap
|
|
4
|
+
module Object
|
|
5
|
+
module Standard
|
|
6
|
+
# REST derived objects
|
|
7
|
+
#
|
|
8
|
+
# Create objects from codestraps builtin remote REST server
|
|
9
|
+
class Rest < Codestrap::Object::Abstract
|
|
10
|
+
class << self
|
|
11
|
+
# Object(s) weight
|
|
12
|
+
# @return [Integer]
|
|
13
|
+
def weight
|
|
14
|
+
50
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def objects
|
|
18
|
+
objects = {}
|
|
19
|
+
if clients
|
|
20
|
+
Array(clients).each do |client|
|
|
21
|
+
objects = client.getobjects
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
objects
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
module Codestrap
|
|
2
|
+
module Patch
|
|
3
|
+
##
|
|
4
|
+
# Inherited and patched Dir class
|
|
5
|
+
#
|
|
6
|
+
# Used as an alternative to monkey patching
|
|
7
|
+
class Dir < Dir
|
|
8
|
+
##
|
|
9
|
+
# List existing directories
|
|
10
|
+
# Patched function
|
|
11
|
+
#
|
|
12
|
+
# @param [Array] dirs
|
|
13
|
+
# @return [Array]
|
|
14
|
+
# Array of directories
|
|
15
|
+
def self.each_dirs_exist(*dirs)
|
|
16
|
+
dirs.select { |dir| File.directory? dir }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
##
|
|
20
|
+
# List existing directory entries
|
|
21
|
+
# Patched function
|
|
22
|
+
#
|
|
23
|
+
# @param [Array] dirs
|
|
24
|
+
# @return [Array]
|
|
25
|
+
# Array of directories full path
|
|
26
|
+
def self.glob_directories(*dirs)
|
|
27
|
+
dirs = dirs[0] if dirs[0].is_a? Array
|
|
28
|
+
list = self.glob(dirs).select { |dir| File.directory? dir }
|
|
29
|
+
if block_given?
|
|
30
|
+
list.each do |dir|
|
|
31
|
+
yield dir
|
|
32
|
+
end
|
|
33
|
+
else
|
|
34
|
+
list
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
##
|
|
39
|
+
# List existing file entries
|
|
40
|
+
# Patched function
|
|
41
|
+
#
|
|
42
|
+
# @param [Array] files
|
|
43
|
+
# @return [Array]
|
|
44
|
+
# Array of directories full path
|
|
45
|
+
def self.glob_files(*files)
|
|
46
|
+
files = files[0] if files[0].is_a? Array
|
|
47
|
+
list = self.glob(files).select { |file| File.file? file }
|
|
48
|
+
if block_given?
|
|
49
|
+
list.each do |file|
|
|
50
|
+
yield file
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
list
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
##
|
|
59
|
+
# Inherited and patched File class
|
|
60
|
+
#
|
|
61
|
+
# Used as an alternative to monkey patching
|
|
62
|
+
class File < File
|
|
63
|
+
##
|
|
64
|
+
#
|
|
65
|
+
# @return [Symbol|nil]
|
|
66
|
+
# Template types :erb
|
|
67
|
+
def template
|
|
68
|
+
@template || begin
|
|
69
|
+
self.mode_line
|
|
70
|
+
@template
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
##
|
|
75
|
+
# Set and get mode line status
|
|
76
|
+
#
|
|
77
|
+
# @param [Integer] limit
|
|
78
|
+
# Number of lines to scan from the top
|
|
79
|
+
# @return [true\nil]
|
|
80
|
+
def mode_line(limit = 10)
|
|
81
|
+
@mode_line ||= begin
|
|
82
|
+
# Check if file read
|
|
83
|
+
curpos = self.pos
|
|
84
|
+
self.pos = 0
|
|
85
|
+
lines = self.readlines()[0 .. (limit - 1)]
|
|
86
|
+
lines.each do |line|
|
|
87
|
+
line =~ /(?:^|\b)(strap|stub):(erb|\S+?)(?:\b|$)/
|
|
88
|
+
if $1
|
|
89
|
+
@mode_line = line
|
|
90
|
+
@template = $2.to_sym if $2
|
|
91
|
+
@template = :erb if $2.length == 0
|
|
92
|
+
self.options
|
|
93
|
+
end
|
|
94
|
+
break
|
|
95
|
+
end
|
|
96
|
+
self.pos = curpos
|
|
97
|
+
@mode_line
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
##
|
|
102
|
+
# Parse mode line options
|
|
103
|
+
def options
|
|
104
|
+
@options ||= begin
|
|
105
|
+
@mode_line.scan(/(\S+)\s*=\s*(\S+)/).each do |arr|
|
|
106
|
+
key = arr[0]
|
|
107
|
+
value = arr[1]
|
|
108
|
+
@options[key] = value
|
|
109
|
+
end
|
|
110
|
+
@options
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
##
|
|
115
|
+
# Has mode line
|
|
116
|
+
# @return [true|false]
|
|
117
|
+
def has_mode_line?
|
|
118
|
+
!!self.mode_line
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'sinatra/base'
|
|
3
|
+
require 'codestrap'
|
|
4
|
+
require 'codestrap/server/version'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'codestrap/config'
|
|
7
|
+
|
|
8
|
+
module Codestrap
|
|
9
|
+
module Server
|
|
10
|
+
class Rest < Sinatra::Base
|
|
11
|
+
|
|
12
|
+
# Settings
|
|
13
|
+
#set :environment, :production
|
|
14
|
+
|
|
15
|
+
# Sessions
|
|
16
|
+
#set :sessions, true
|
|
17
|
+
|
|
18
|
+
# Logging to STDERR
|
|
19
|
+
set :logging, false
|
|
20
|
+
|
|
21
|
+
# Applications root directory
|
|
22
|
+
#set :root, 'path/to/root'
|
|
23
|
+
|
|
24
|
+
# Serve files from public directory
|
|
25
|
+
set :static, false
|
|
26
|
+
|
|
27
|
+
# Public folder
|
|
28
|
+
#set :public_folder, 'path/to/static/files'
|
|
29
|
+
|
|
30
|
+
# Views folder defaults to static_dir/views
|
|
31
|
+
#set :views, Proc.new { File.join(dir, 'templates')}
|
|
32
|
+
|
|
33
|
+
# Enable/disable built in web server
|
|
34
|
+
#set :run, false
|
|
35
|
+
|
|
36
|
+
# Built in web servers. Following is default
|
|
37
|
+
#set :server, %w[thin mongrel webrick]
|
|
38
|
+
|
|
39
|
+
# Binding
|
|
40
|
+
#set :bind, '0.0.0.0'
|
|
41
|
+
|
|
42
|
+
# Set port
|
|
43
|
+
#set :port, 9494
|
|
44
|
+
|
|
45
|
+
# Set app file. Calculates :root, :public_folder, :views
|
|
46
|
+
#set :app_file, __FILE__
|
|
47
|
+
|
|
48
|
+
# Propagate errors to server or rack
|
|
49
|
+
#set :raise_errors
|
|
50
|
+
|
|
51
|
+
# Show error pages
|
|
52
|
+
#set :show_exceptions, true
|
|
53
|
+
|
|
54
|
+
version = Codestrap::Server::VERSION
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
# TODO - hardcode2config Host to advertise
|
|
58
|
+
host = 'codestrap.test.domain'
|
|
59
|
+
|
|
60
|
+
# TODO - hardcode2config port to advertise
|
|
61
|
+
port ||= settings.port
|
|
62
|
+
|
|
63
|
+
# TODO - hardcode2config Schema to advertise. http or https
|
|
64
|
+
scheme = 'http'
|
|
65
|
+
|
|
66
|
+
# Objects
|
|
67
|
+
urlobjects = '/rest/objects.json'
|
|
68
|
+
get urlobjects do
|
|
69
|
+
codestrap = Codestrap::Core.new()
|
|
70
|
+
obj = Codestrap::Object::Factory.new
|
|
71
|
+
obj.dirs = codestrap.config.server.objects
|
|
72
|
+
content_type :json
|
|
73
|
+
case params[:prettyprint]
|
|
74
|
+
when 'true'
|
|
75
|
+
JSON.pretty_generate(obj.to_hash)
|
|
76
|
+
else
|
|
77
|
+
obj.to_hash.to_json
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# BP metadata
|
|
82
|
+
urlstrapmetadata = '/rest/strap/metadata.json'
|
|
83
|
+
get urlstrapmetadata do
|
|
84
|
+
name = params[:name]
|
|
85
|
+
codestrap = Codestrap::Core.new()
|
|
86
|
+
paths = codestrap.config.server.content
|
|
87
|
+
output = codestrap.strap_metadata(name, nil, paths)
|
|
88
|
+
content_type :json
|
|
89
|
+
case params[:prettyprint]
|
|
90
|
+
when 'true'
|
|
91
|
+
JSON.pretty_generate(output)
|
|
92
|
+
else
|
|
93
|
+
output.to_json
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Codestrap metadata
|
|
98
|
+
urlcodestrapmetadata = '/rest/stub/metadata.json'
|
|
99
|
+
get urlcodestrapmetadata do
|
|
100
|
+
name = params[:name]
|
|
101
|
+
codestrap = Codestrap::Core.new()
|
|
102
|
+
paths = codestrap.config.server.content
|
|
103
|
+
output = codestrap.stub_metadata(name, nil, paths)
|
|
104
|
+
content_type :json
|
|
105
|
+
case params[:prettyprint]
|
|
106
|
+
when 'true'
|
|
107
|
+
JSON.pretty_generate(output)
|
|
108
|
+
else
|
|
109
|
+
output.to_json
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Boilerplate content
|
|
114
|
+
urlstrapproject = '/rest/strap/project'
|
|
115
|
+
get "#{urlstrapproject}/:name/*" do
|
|
116
|
+
# TODO - Rest error message
|
|
117
|
+
# TODO - Logging
|
|
118
|
+
# TODO - Handle all files
|
|
119
|
+
name = params[:name]
|
|
120
|
+
data_file = params[:splat].first
|
|
121
|
+
file_path = nil
|
|
122
|
+
codestrap = Codestrap::Core.new()
|
|
123
|
+
paths = codestrap.config.server.content
|
|
124
|
+
codestrap.strap_metadata(name, {src: true, ftype: true, mode: true}, paths).values.each do |data|
|
|
125
|
+
data[:files].each do |file|
|
|
126
|
+
next unless File.expand_path(file[:file]).eql?(File.expand_path(data_file))
|
|
127
|
+
file_path = file[:src]
|
|
128
|
+
break
|
|
129
|
+
end
|
|
130
|
+
break
|
|
131
|
+
end
|
|
132
|
+
send_file file_path
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# File content
|
|
136
|
+
urlcodestrapfile = '/rest/stub/file'
|
|
137
|
+
get "#{urlcodestrapfile}/:name" do
|
|
138
|
+
# TODO - Rest error message
|
|
139
|
+
# TODO - Logging
|
|
140
|
+
# TODO - Handle all files
|
|
141
|
+
name = params[:name]
|
|
142
|
+
codestrap = Codestrap::Core.new()
|
|
143
|
+
paths = codestrap.config.server.content
|
|
144
|
+
file_path = codestrap.stub_metadata(name, {src: true, ftype: true, mode: true}, paths)[name][:src]
|
|
145
|
+
send_file file_path
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
cap = {
|
|
149
|
+
version: version,
|
|
150
|
+
urls: {
|
|
151
|
+
objects: urlobjects,
|
|
152
|
+
stub: {
|
|
153
|
+
metadata: urlcodestrapmetadata,
|
|
154
|
+
file: urlcodestrapfile,
|
|
155
|
+
},
|
|
156
|
+
strap: {
|
|
157
|
+
metadata: urlstrapmetadata,
|
|
158
|
+
project: urlstrapproject
|
|
159
|
+
}
|
|
160
|
+
# TODO - Packages
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
# Capabilities
|
|
165
|
+
get '/rest/capability.json' do
|
|
166
|
+
content_type :json
|
|
167
|
+
case params[:prettyprint]
|
|
168
|
+
when 'true'
|
|
169
|
+
JSON.pretty_generate(cap)
|
|
170
|
+
else
|
|
171
|
+
cap.to_json
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|