thanthus 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,179 @@
1
+ require 'fileutils'
2
+ require 'json'
3
+
4
+ module Xanthus
5
+ class Dataverse < Repository
6
+ attr_accessor :server
7
+ attr_accessor :repo
8
+ attr_accessor :token
9
+ attr_accessor :dataset_name
10
+ attr_accessor :author
11
+ attr_accessor :affiliation
12
+ attr_accessor :email
13
+ attr_accessor :description
14
+ attr_accessor :subject
15
+ attr_accessor :doi
16
+
17
+ def initialize
18
+ super
19
+ @server = 'default_server'
20
+ @repo = 'default_repo'
21
+ @token = 'default_token'
22
+ @dataset_name = 'default_name'
23
+ @author = 'default_author'
24
+ @affiliation = 'default_affiliation'
25
+ @email = 'default_email'
26
+ @description = 'default_description'
27
+ @subject = 'default_subject'
28
+ @doi = 'not_set'
29
+ end
30
+
31
+ def dataset_json
32
+ json = %Q{
33
+ {
34
+ "datasetVersion": {
35
+ "metadataBlocks": {
36
+ "citation": {
37
+ "fields": [
38
+ {
39
+ "value": "#{@dataset_name}",
40
+ "typeClass": "primitive",
41
+ "multiple": false,
42
+ "typeName": "title"
43
+ },
44
+ {
45
+ "value": [
46
+ {
47
+ "authorName": {
48
+ "value": "#{@author}",
49
+ "typeClass": "primitive",
50
+ "multiple": false,
51
+ "typeName": "authorName"
52
+ },
53
+ "authorAffiliation": {
54
+ "value": "#{@affiliation}",
55
+ "typeClass": "primitive",
56
+ "multiple": false,
57
+ "typeName": "authorAffiliation"
58
+ }
59
+ }
60
+ ],
61
+ "typeClass": "compound",
62
+ "multiple": true,
63
+ "typeName": "author"
64
+ },
65
+ {
66
+ "value": [
67
+ { "datasetContactEmail" : {
68
+ "typeClass": "primitive",
69
+ "multiple": false,
70
+ "typeName": "datasetContactEmail",
71
+ "value" : "#{@email}"
72
+ },
73
+ "datasetContactName" : {
74
+ "typeClass": "primitive",
75
+ "multiple": false,
76
+ "typeName": "datasetContactName",
77
+ "value": "#{@author}"
78
+ }
79
+ }],
80
+ "typeClass": "compound",
81
+ "multiple": true,
82
+ "typeName": "datasetContact"
83
+ },
84
+ {
85
+ "value": [ {
86
+ "dsDescriptionValue":{
87
+ "value": "#{@description.gsub(/\r/," ").gsub(/\n/," ")}",
88
+ "multiple":false,
89
+ "typeClass": "primitive",
90
+ "typeName": "dsDescriptionValue"
91
+ }}],
92
+ "typeClass": "compound",
93
+ "multiple": true,
94
+ "typeName": "dsDescription"
95
+ },
96
+ {
97
+ "value": [
98
+ "#{@subject}"
99
+ ],
100
+ "typeClass": "controlledVocabulary",
101
+ "multiple": true,
102
+ "typeName": "subject"
103
+ }
104
+ ],
105
+ "displayName": "Citation Metadata"
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ return json
112
+ end
113
+
114
+ def create_dataset
115
+ Dir.chdir 'dataverse_dataset' do
116
+ File.open('dataset.json', 'w+') do |f|
117
+ f.write(self.dataset_json)
118
+ end
119
+ puts "Creating dataverse #{@dataset_name} in #{@repo} at #{@server}..."
120
+ output = `curl --speed-time 15 --speed-limit 1000 --retry 50 --retry-max-time 0 -H X-Dataverse-key:#{@token} -X POST #{@server}/api/dataverses/#{@repo}/datasets --upload-file dataset.json`
121
+ puts output # needed to escape curl output
122
+ parsed = JSON.parse(output)
123
+ @doi = parsed['data']['persistentId']
124
+ puts "Dataverse #{@doi} created."
125
+ end
126
+ end
127
+
128
+ def init config
129
+ # initialize with config information
130
+ @author = config.authors
131
+ @affiliation = config.affiliation
132
+ @email = config.email
133
+ @description = config.description
134
+ @dataset_name = config.name+'-'+Time.now.strftime("%Y-%m-%d_%H-%M")
135
+
136
+ FileUtils.mkdir_p 'dataverse_dataset'
137
+ self.create_dataset
138
+ Dir.chdir 'dataverse_dataset' do
139
+ FileUtils.mkdir_p 'repo'
140
+ Dir.chdir 'repo' do
141
+ self.xanthus_file
142
+ self.readme_file config
143
+ self.inputs_file config
144
+ end
145
+ end
146
+ end
147
+
148
+ def add_file_to_dataverse name, description, folder
149
+ output = `curl --speed-time 15 --speed-limit 1000 --retry 50 --retry-max-time 0 -H X-Dataverse-key:#{@token} -X POST -F "file=@#{name}" -F 'jsonData={"description":"#{description}","directoryLabel":"#{folder}","categories":["Data"], "restrict":"false"}' "#{@server}/api/datasets/:persistentId/add?persistentId=#{@doi}"`
150
+ puts output
151
+ end
152
+
153
+ def xanthus_file
154
+ self.prepare_xanthus_file
155
+ self.add_file_to_dataverse '.xanthus', 'xanthus file used to generate the data.', 'metadata'
156
+ end
157
+
158
+ def readme_file config
159
+ self.prepare_readme_file config
160
+ self.add_file_to_dataverse 'README.md', 'readme describing the dataset.', 'metadata'
161
+ end
162
+
163
+ def inputs_file config
164
+ config.jobs.each do |name,job|
165
+ job.inputs.each do |k, files|
166
+ files.each do |file|
167
+ system('cp', '-f', "../../#{file}", "#{file}")
168
+ self.add_file_to_dataverse file, 'Job input file.', 'metadata'
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ def add content
175
+ self.add_file_to_dataverse content, "#{content} is a Xanthus generated file (check metadata for description)", 'data'
176
+ end
177
+
178
+ end
179
+ end
@@ -0,0 +1,21 @@
1
+ module Xanthus
2
+ CAMFLOW_START = %q{%{
3
+ camflow -a true
4
+ sleep 1
5
+ }}
6
+
7
+ CAMFLOW_STOP = %q{%{
8
+ camflow -a false
9
+ sleep 20
10
+ }}
11
+
12
+ SPADE_START = %q{%{
13
+ echo spade | sudo -H -u spade ../SPADE/bin/spade start
14
+ sleep 20
15
+ }}
16
+
17
+ SPADE_STOP = %q{%{
18
+ echo spade | sudo -H -u spade ../SPADE/bin/spade stop
19
+ sleep 20
20
+ }}
21
+ end
@@ -0,0 +1,90 @@
1
+ require 'fileutils'
2
+
3
+ module Xanthus
4
+ class GitHub < Repository
5
+ attr_accessor :repo
6
+ attr_accessor :token
7
+ attr_accessor :folder
8
+
9
+ def initialize
10
+ super
11
+ @repo = ''
12
+ @token = ''
13
+ @folder = Time.now.strftime("%Y-%m-%d_%H-%M")
14
+ end
15
+
16
+ def lfs
17
+ system('git', 'lfs', 'install')
18
+ system('git', 'lfs', 'track', '*.tar.gz')
19
+ system('git', 'add', '.gitattributes')
20
+ system('git', 'push', "https://#{@token}@github.com/#{@repo}", 'master')
21
+ end
22
+
23
+ def xanthus_file
24
+ self.prepare_xanthus_file
25
+ system('git', 'add', '.xanthus')
26
+ system('git', 'commit', '-m', "[Xanthus] :horse: pushed #{@folder}/.xanthus :horse:")
27
+ system('git', 'push', "https://#{@token}@github.com/#{@repo}", 'master')
28
+ end
29
+
30
+ def readme_file config
31
+ self.prepare_readme_file config
32
+ system('git', 'add', 'README.md')
33
+ system('git', 'commit', '-m', "[Xanthus] :horse: pushed #{@folder}/README.md :horse:")
34
+ system('git', 'push', "https://#{@token}@github.com/#{@repo}", 'master')
35
+ end
36
+
37
+ def inputs_file config
38
+ config.jobs.each do |name,job|
39
+ job.inputs.each do |k, files|
40
+ files.each do |file|
41
+ system('cp', '-f', "../../#{file}", "#{file}")
42
+ system('git', 'add', "#{file}")
43
+ system('git', 'commit', '-m', "[Xanthus] :horse: pushed #{@folder}/#{file} :horse:")
44
+ system('git', 'push', "https://#{@token}@github.com/#{@repo}", 'master')
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def init config
51
+ system('git', 'clone', "https://#{@token}@github.com/#{@repo}", 'repo')
52
+ Dir.chdir 'repo' do
53
+ self.lfs
54
+ FileUtils.mkdir_p @folder
55
+ Dir.chdir @folder do
56
+ self.xanthus_file
57
+ self.readme_file config
58
+ self.inputs_file config
59
+ end
60
+ end
61
+ end
62
+
63
+ def add content
64
+ Dir.chdir 'repo' do
65
+ FileUtils.mkdir_p @folder
66
+ system('mv', "../#{content}", "#{@folder}/#{content}")
67
+ system('git', 'add', "#{@folder}/#{content}")
68
+ system('git', 'commit', '-m', "[Xanthus] :horse: pushed #{@folder}/#{content} :horse:")
69
+ end
70
+ end
71
+
72
+ def push
73
+ Dir.chdir 'repo' do
74
+ system('git', 'push', "https://#{@token}@github.com/#{@repo}", 'master')
75
+ system('rm', '-rf', @folder)
76
+ end
77
+ end
78
+
79
+ def tag
80
+ Dir.chdir 'repo' do
81
+ system('git', 'tag', '-a', "xanthus-#{@folder}", '-m', '"Xanthus automated dataset generation."')
82
+ system('git', 'push', '--tags', "https://#{@token}@github.com/#{@repo}")
83
+ end
84
+ end
85
+
86
+ def clean
87
+ system('rm', '-rf', 'repo')
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,156 @@
1
+ require 'fileutils'
2
+
3
+ module Xanthus
4
+ class Init
5
+ @@name
6
+
7
+ def self.header file
8
+ file.write("# -*- mode: ruby -*-\n")
9
+ file.write("# vi: set ft=ruby\n\n")
10
+ end
11
+
12
+ def self.config file
13
+ script = %Q{
14
+ # -*- mode: ruby -*-
15
+ # vi: set ft=ruby
16
+
17
+ Xanthus.configure do |config|
18
+ config.name = '#{@@name}'
19
+ config.authors = 'John Doe'
20
+ config.affiliation = 'Somewhere University'
21
+ config.email = 'john.doe@somewhere.edu'
22
+ config.description = %q{
23
+ Describe my super experiment.
24
+
25
+ It is very cool and interesting!
26
+ }
27
+ config.seed = #{Random.new_seed}
28
+
29
+ config.script :pre do
30
+ %q{%{
31
+ mkdir wgets
32
+ cd wgets
33
+ }}
34
+ end
35
+
36
+ config.script :camflow_start do
37
+ Xanthus::CAMFLOW_START
38
+ end
39
+
40
+ config.script :spade_start do
41
+ Xanthus::SPADE_START
42
+ end
43
+
44
+ config.script :normal do
45
+ %q{
46
+ 2.times.collect do
47
+ 'wget http://www.google.com'
48
+ end
49
+ }
50
+ end
51
+
52
+ config.script :attack do
53
+ %q{
54
+ 2.times.collect do
55
+ 'wget http://www.google.com'
56
+ end
57
+ }
58
+ end
59
+
60
+ config.script :camflow_stop do
61
+ Xanthus::CAMFLOW_STOP
62
+ end
63
+
64
+ config.script :spade_stop do
65
+ Xanthus::SPADE_STOP
66
+ end
67
+
68
+ config.script :post do
69
+ %q{%{
70
+ cd ..
71
+ rm -rf wgets
72
+ }}
73
+ end
74
+
75
+ config.script :server do
76
+ %q{%{
77
+ mkdir test
78
+ }}
79
+ end
80
+
81
+ config.vm :camflow do |vm|
82
+ vm.box = 'michaelh/ubuncam'
83
+ vm.version = '0.0.3'
84
+ vm.ip = '192.168.33.8'
85
+ end
86
+
87
+ config.vm :spade do |vm|
88
+ vm.box = 'michaelh/spade'
89
+ vm.memory = 8192
90
+ vm.version = '0.0.3'
91
+ vm.ip = '192.168.33.8'
92
+ end
93
+
94
+ config.vm :server do |vm|
95
+ vm.box = 'bento/ubuntu-18.04'
96
+ vm.version = '201812.27.0'
97
+ vm.ip = '192.168.33.3'
98
+ end
99
+
100
+ config.job :normal_camflow do |job|
101
+ job.iterations = 2
102
+ job.tasks = {camflow: [:pre, :camflow_start, :normal, :camflow_stop, :post]}
103
+ job.outputs = {camflow: {config: '/etc/camflow.ini', trace: '/tmp/audit.log'}}
104
+ end
105
+
106
+ config.job :attack_camflow do |job|
107
+ job.iterations = 2
108
+ job.tasks = {server: [:server], camflow: [:pre, :camflow_start, :attack, :camflow_stop, :post]}
109
+ job.outputs = {camflow: {config: '/etc/camflow.ini', trace: '/tmp/audit.log'}}
110
+ end
111
+
112
+ config.job :normal_spade do |job|
113
+ job.iterations = 2
114
+ job.tasks = {spade: [:pre, :spade_start, :normal, :spade_stop, :post]}
115
+ job.outputs = {spade: {trace: '/tmp/audit_cdm.avro'}}
116
+ end
117
+
118
+ config.job :attack_spade do |job|
119
+ job.iterations = 2
120
+ job.tasks = {server: [:server], spade: [:pre, :spade_start, :attack, :spade_stop, :post]}
121
+ job.outputs = {spade: {trace: '/tmp/audit_cdm.avro'}}
122
+ end
123
+
124
+ config.dataverse do |dataverse|
125
+ dataverse.server = <ADD DATAVERSE BASE URL>
126
+ dataverse.repo = <PROVIDE DATAVERSE NAME>
127
+ dataverse.token = <PROVIDE DATAVERSE TOKEN>
128
+ dataverse.subject = <PROVIDE DATAVERSE SUBJECT (e.g. engineering)>
129
+ end
130
+
131
+ # config.github do |github|
132
+ # github.repo = '<ADD GITHUB REPO user/name>'
133
+ # github.token = '<ADD GITHUB TOKEN>'
134
+ # end
135
+ end
136
+ }
137
+ file.write(script)
138
+ end
139
+
140
+ def self.init name
141
+ @@name = name
142
+ abort("Error: #{@@name} already exists.") unless !File.exists? name
143
+ FileUtils.mkdir_p @@name
144
+ Dir.chdir @@name do
145
+ puts "Creating experiment #{@@name}..."
146
+ File.open('.xanthus', 'w+') do |f|
147
+ self.header f
148
+ self.config f
149
+ end
150
+ end
151
+ puts 'Experiment created.'
152
+ puts "Edit #{@@name}/.xanthus to configure your experiment."
153
+ puts 'To run your experiment "xanthus run".'
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,135 @@
1
+ require 'fileutils'
2
+ require_relative '../utils/os'
3
+
4
+ module Xanthus
5
+ class Job
6
+ attr_accessor :name
7
+ attr_accessor :iterations
8
+ attr_accessor :tasks
9
+ attr_accessor :outputs
10
+ attr_accessor :inputs
11
+ attr_accessor :pre_instructions
12
+ attr_accessor :post_instructions
13
+
14
+ def initialize
15
+ @iterations = 0
16
+ @tasks = Hash.new
17
+ @outputs = Hash.new
18
+ @inputs = Hash.new
19
+ @pre_instructions = nil
20
+ @post_instructions = nil
21
+ end
22
+
23
+ def output_script machine, outputs
24
+ script = "vagrant plugin install vagrant-scp\n"
25
+ outputs.each do |name, path|
26
+ script += "vagrant scp :#{path} output/#{name}.data\n"
27
+ end
28
+ return script
29
+ end
30
+
31
+ def setup_env machine, scripts, config
32
+ puts 'Setting up task on machine '+machine.to_s+'...'
33
+ FileUtils.mkdir_p machine.to_s
34
+ Dir.chdir machine.to_s do
35
+ if !@inputs[machine].nil?
36
+ @inputs[machine].each do |name|
37
+ system('cp', '-f', "../../#{name}", "#{name}")
38
+ end
39
+ end
40
+ FileUtils.mkdir_p 'output'
41
+ puts 'Creating provision files...'
42
+ File.open('Vagrantfile', 'w+') do |f|
43
+ f.write(config.vms[machine].to_vagrant)
44
+ end
45
+ script = Script.new(scripts, config).to_s
46
+ File.open('provision.sh', 'w+') do |f|
47
+ f.write(script)
48
+ end
49
+ script = 'echo "nothing to do"'
50
+ script = self.output_script(machine, @outputs[machine]) unless @outputs[machine].nil?
51
+ # add simple support for Windows
52
+ before_halt_hook = "before_halt.#{sys_script_ext}"
53
+ File.open(before_halt_hook, 'w+') do |f|
54
+ f.write(script)
55
+ end
56
+ system('chmod', '+x', before_halt_hook)
57
+ end
58
+ end
59
+
60
+ def host_scripts config
61
+ puts 'Setting up host scripts...'
62
+ if !@pre_instructions.nil?
63
+ script = Script.new(@pre_instructions, config).to_s
64
+ File.open('pre.sh', 'w+') do |f|
65
+ f.write(script)
66
+ end
67
+ end
68
+
69
+ if !@post_instructions.nil?
70
+ script = Script.new(@post_instructions, config).to_s
71
+ File.open('post.sh', 'w+') do |f|
72
+ f.write(script)
73
+ end
74
+ end
75
+ end
76
+
77
+ def execute_pre_instructions
78
+ puts 'Running pre instructions...'
79
+ system('sh', './pre.sh')
80
+ end
81
+
82
+ def run machine
83
+ Dir.chdir machine.to_s do
84
+ system('vagrant', 'up')
85
+ end
86
+ end
87
+
88
+ def execute_post_instructions
89
+ puts 'Running post instructions...'
90
+ system('sh', './post.sh')
91
+ end
92
+
93
+ def halt machine
94
+ Dir.chdir machine.to_s do
95
+ system('vagrant', 'halt')
96
+ end
97
+ end
98
+
99
+ def destroy machine
100
+ Dir.chdir machine.to_s do
101
+ system('vagrant', 'destroy', '-f')
102
+ system('rm', '-rf', '.vagrant')
103
+ end
104
+ end
105
+
106
+ def execute config, iteration
107
+ puts "Running job #{name.to_s}-#{iteration.to_s}..."
108
+ FileUtils.mkdir_p 'tmp'
109
+ Dir.chdir 'tmp' do
110
+ self.host_scripts config
111
+ @tasks.each do |machine, templates|
112
+ self.setup_env machine, templates, config
113
+ end
114
+ self.execute_pre_instructions unless @pre_instructions.nil?
115
+ @tasks.each do |machine, templates|
116
+ self.run machine
117
+ end
118
+ self.execute_post_instructions unless @post_instructions.nil?
119
+ @tasks.each do |machine, templates|
120
+ self.halt machine
121
+ end
122
+ @tasks.each do |machine, templates|
123
+ self.destroy machine
124
+ end
125
+ end
126
+ system('mv', 'tmp', "#{name.to_s}-#{iteration.to_s}")
127
+ system('tar', '-czvf', "#{name.to_s}-#{iteration.to_s}.tar.gz", "#{name.to_s}-#{iteration.to_s}")
128
+ system('rm', '-rf', "#{name.to_s}-#{iteration.to_s}")
129
+ config.github_conf.add("#{name.to_s}-#{iteration.to_s}.tar.gz") unless config.github_conf.nil?
130
+ config.github_conf.push unless config.github_conf.nil?
131
+ config.dataverse_conf.add("#{name.to_s}-#{iteration.to_s}.tar.gz") unless config.dataverse_conf.nil?
132
+ puts "Job #{name.to_s}-#{iteration.to_s} done."
133
+ end
134
+ end
135
+ end