antfarm-core 0.5.0.beta1

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.
@@ -0,0 +1,51 @@
1
+ ################################################################################
2
+ # #
3
+ # Copyright (2008-2010) Sandia Corporation. Under the terms of Contract #
4
+ # DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains #
5
+ # certain rights in this software. #
6
+ # #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy #
8
+ # of this software and associated documentation files (the "Software"), to #
9
+ # deal in the Software without restriction, including without limitation the #
10
+ # rights to use, copy, modify, merge, publish, distribute, distribute with #
11
+ # modifications, sublicense, and/or sell copies of the Software, and to permit #
12
+ # persons to whom the Software is furnished to do so, subject to the following #
13
+ # conditions: #
14
+ # #
15
+ # The above copyright notice and this permission notice shall be included in #
16
+ # all copies or substantial portions of the Software. #
17
+ # #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
21
+ # ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR #
23
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
24
+ # SOFTWARE. #
25
+ # #
26
+ # Except as contained in this notice, the name(s) of the above copyright #
27
+ # holders shall not be used in advertising or otherwise to promote the sale, #
28
+ # use or other dealings in this Software without prior written authorization. #
29
+ # #
30
+ ################################################################################
31
+
32
+ require 'data_objects'
33
+ require 'do_sqlite3'
34
+
35
+ require 'dm-constraints'
36
+ require 'dm-validations'
37
+
38
+ # require all the models
39
+ require 'antfarm/models/ethernet_interface'
40
+ require 'antfarm/models/ip_interface'
41
+ require 'antfarm/models/ip_network'
42
+ require 'antfarm/models/layer_two_interface'
43
+ require 'antfarm/models/layer_three_interface'
44
+ require 'antfarm/models/layer_three_network'
45
+ require 'antfarm/models/node'
46
+ # require 'antfarm/models/private_network'
47
+ # require 'antfarm/models/traffic'
48
+ # require 'antfarm/models/dns_entry'
49
+ # require 'antfarm/models/action'
50
+ # require 'antfarm/models/service'
51
+ # require 'antfarm/models/operating_system'
@@ -0,0 +1,84 @@
1
+ ################################################################################
2
+ # #
3
+ # Copyright (2008-2010) Sandia Corporation. Under the terms of Contract #
4
+ # DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains #
5
+ # certain rights in this software. #
6
+ # #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy #
8
+ # of this software and associated documentation files (the "Software"), to #
9
+ # deal in the Software without restriction, including without limitation the #
10
+ # rights to use, copy, modify, merge, publish, distribute, distribute with #
11
+ # modifications, sublicense, and/or sell copies of the Software, and to permit #
12
+ # persons to whom the Software is furnished to do so, subject to the following #
13
+ # conditions: #
14
+ # #
15
+ # The above copyright notice and this permission notice shall be included in #
16
+ # all copies or substantial portions of the Software. #
17
+ # #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
21
+ # ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR #
23
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
24
+ # SOFTWARE. #
25
+ # #
26
+ # Except as contained in this notice, the name(s) of the above copyright #
27
+ # holders shall not be used in advertising or otherwise to promote the sale, #
28
+ # use or other dealings in this Software without prior written authorization. #
29
+ # #
30
+ ################################################################################
31
+
32
+ module Antfarm
33
+ module Model
34
+ class EthernetInterface
35
+ include DataMapper::Resource
36
+
37
+ storage_names[:default] = 'ethernet_interfaces'
38
+
39
+ property :id, Serial
40
+ property :address, String, :required => true
41
+ property :custom, String
42
+
43
+ # See the IpInterface model for an explanation of why we're doing this.
44
+ property :layer_two_interface_id, Integer, :required => true, :auto_validation => false
45
+
46
+ belongs_to :layer_two_interface #, :required => true, :auto_validation => false
47
+
48
+ before :create, :create_layer_two_interface
49
+
50
+ # This ensures that the MAC address entered
51
+ validates_format :address, :with => %r{\A([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}\z},
52
+ :message => 'invalid MAC address format'
53
+
54
+ #######
55
+ private
56
+ #######
57
+
58
+ # If a hash is passed into the layer2_interface
59
+ # variable, parameters matching variables on the
60
+ # layer 2 interface class will be used to create
61
+ # a new layer 2 interface object. This also works
62
+ # for nested hashes - i.e. if the hash passed into
63
+ # the layer2_interface variable contains a 'node'
64
+ # key that has a hash as a value, parameters in
65
+ # that hash that match variables on the node class
66
+ # will be used to create the node created by the
67
+ # layer 2 interface. w00t!
68
+ def create_layer_two_interface
69
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] EthernetInterface#create_layer_two_interface'
70
+
71
+ # Only create a new layer 2 interface if
72
+ # a layer 2 interface model isn't already
73
+ # associated with this model. This protects
74
+ # against new layer 2 interfaces being
75
+ # created when one is already provided or
76
+ # when this model is being saved rather
77
+ # than created (since a layer 2 interface
78
+ # will be automatically created and associated
79
+ # with this model on creation).
80
+ self.layer_two_interface ||= Antfarm::Model::LayerTwoInterface.create
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,134 @@
1
+ ################################################################################
2
+ # #
3
+ # Copyright (2008-2010) Sandia Corporation. Under the terms of Contract #
4
+ # DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains #
5
+ # certain rights in this software. #
6
+ # #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy #
8
+ # of this software and associated documentation files (the "Software"), to #
9
+ # deal in the Software without restriction, including without limitation the #
10
+ # rights to use, copy, modify, merge, publish, distribute, distribute with #
11
+ # modifications, sublicense, and/or sell copies of the Software, and to permit #
12
+ # persons to whom the Software is furnished to do so, subject to the following #
13
+ # conditions: #
14
+ # #
15
+ # The above copyright notice and this permission notice shall be included in #
16
+ # all copies or substantial portions of the Software. #
17
+ # #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
21
+ # ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR #
23
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
24
+ # SOFTWARE. #
25
+ # #
26
+ # Except as contained in this notice, the name(s) of the above copyright #
27
+ # holders shall not be used in advertising or otherwise to promote the sale, #
28
+ # use or other dealings in this Software without prior written authorization. #
29
+ # #
30
+ ################################################################################
31
+
32
+ module Antfarm
33
+ module Model
34
+ class IpInterface
35
+ include DataMapper::Resource
36
+
37
+ storage_names[:default] = 'ip_interfaces'
38
+
39
+ property :id, Serial
40
+
41
+ # I turn auto_validation off here since we are manually
42
+ # validating that an address was provided below in the
43
+ # 'validates_with_block' declaration below. Read below
44
+ # for why we do this manually...
45
+ property :address, String, :required => true, :auto_validation => false
46
+ property :virtual, Boolean, :required => true, :default => false
47
+ property :custom, String
48
+
49
+ # So right now we have a problem... currently we do NOT
50
+ # want to create a layer 3 interface (and consequently
51
+ # an IP network and layer 3 network) if the data given
52
+ # for a new IP interface is not valid. This can be done
53
+ # by requiring a layer_three_interface_id to exist for
54
+ # an IpInterface, but not auto-validating, which will
55
+ # allow us to validate the data given BEFORE creating
56
+ # a new layer 3 interface. However, currently the
57
+ # 'belongs_to' method does not act on ':auto_validation'
58
+ # (it will in a future dm-validations release) so we
59
+ # have to specify the 'layer_three_interface_id' property
60
+ # explicitly here until the new version is out. For more
61
+ # information, see the GitHub Gist and comments between
62
+ # dkubb and ccss-sandia.
63
+ property :layer_three_interface_id, Integer, :required => true, :auto_validation => false
64
+
65
+ belongs_to :layer_three_interface #, :required => true, :auto_validation => false
66
+
67
+ before :create, :create_layer_three_interface
68
+
69
+ # validates_format :address,
70
+ # :with => %r{\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}(?:\/[1-3]\d)?\z},
71
+ # :message => 'invalid IP address format'
72
+
73
+ # We're doing all the validations here manually
74
+ # (rather than using the shortcuts above) so they
75
+ # will be executed in order. If the shortcut methods
76
+ # are used, this validation is ran first so when
77
+ # an invalid address is given the @ip_addr object
78
+ # doesn't exist and NoMethodErrors are thrown.
79
+ validates_with_block :address do
80
+ format = %r{\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}(?:\/[1-3]\d)?\z}
81
+ @ip_addr ||= Antfarm::IPAddrExt.new(self.address) rescue nil
82
+
83
+ if self.address.nil?
84
+ [ false, 'must be present' ]
85
+ elsif not self.address =~ format
86
+ [ false, 'invalid IP address format' ]
87
+ elsif @ip_addr.loopback_address?
88
+ [ false, 'loopback address not allowed' ]
89
+ elsif not @ip_addr.private_address?
90
+ # If the address is public and it already exists in the database, don't create
91
+ # a new one but still create a new IP Network just in case the data given for
92
+ # this address includes more detailed information about its network.
93
+ #
94
+ # TODO: what should we do about multiple private networks?
95
+ # Right now, multiple private interfaces of the same address can exist,
96
+ # but they will just be associated with the same private network...
97
+ if interface = Antfarm::Model::IpInterface.find_by_address(self.address)
98
+ create_ip_network
99
+ [ false, "#{self.address} already exits, but a new IP network was created anyway" ]
100
+ else
101
+ true
102
+ end
103
+ else
104
+ true
105
+ end
106
+ end
107
+
108
+ #######
109
+ private
110
+ #######
111
+
112
+ def create_layer_three_interface
113
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] IpInterface#create_layer_three_interface'
114
+
115
+ self.layer_three_interface ||= Antfarm::Model::LayerThreeInterface.create :layer_three_network => create_ip_network
116
+ end
117
+
118
+ def create_ip_network
119
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] IpInterface#create_ip_network'
120
+
121
+ @ip_addr ||= Antfarm::IPAddrExt.new(self.address) rescue return nil
122
+
123
+ network = Antfarm::Model::LayerThreeNetwork.network_containing(@ip_addr.to_cidr_string)
124
+ return network unless network.nil?
125
+
126
+ net = @ip_addr.clone
127
+ net.netmask = net.netmask << 2 if net == net.network
128
+
129
+ network = Antfarm::Model::IpNetwork.create :address => net.to_cidr_string
130
+ return network.layer_three_network
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,118 @@
1
+ ################################################################################
2
+ # #
3
+ # Copyright (2008-2010) Sandia Corporation. Under the terms of Contract #
4
+ # DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains #
5
+ # certain rights in this software. #
6
+ # #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy #
8
+ # of this software and associated documentation files (the "Software"), to #
9
+ # deal in the Software without restriction, including without limitation the #
10
+ # rights to use, copy, modify, merge, publish, distribute, distribute with #
11
+ # modifications, sublicense, and/or sell copies of the Software, and to permit #
12
+ # persons to whom the Software is furnished to do so, subject to the following #
13
+ # conditions: #
14
+ # #
15
+ # The above copyright notice and this permission notice shall be included in #
16
+ # all copies or substantial portions of the Software. #
17
+ # #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
21
+ # ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR #
23
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
24
+ # SOFTWARE. #
25
+ # #
26
+ # Except as contained in this notice, the name(s) of the above copyright #
27
+ # holders shall not be used in advertising or otherwise to promote the sale, #
28
+ # use or other dealings in this Software without prior written authorization. #
29
+ # #
30
+ ################################################################################
31
+
32
+ module Antfarm
33
+ module Model
34
+ class IpNetwork
35
+ include DataMapper::Resource
36
+
37
+ storage_names[:default] = 'ip_networks'
38
+
39
+ property :id, Serial
40
+
41
+ # I turn auto_validation off here since we are manually
42
+ # validating that an address was provided below in the
43
+ # 'validates_with_block' declaration below. Read below
44
+ # for why we do this manually...
45
+ property :address, String, :required => true, :auto_validation => false
46
+ property :private, Boolean, :required => true, :default => false
47
+ property :custom, String
48
+
49
+ # See the IpInterface model for an explanation of why we're doing this.
50
+ property :layer_three_network_id, Integer, :required => true, :auto_validation => false
51
+
52
+ belongs_to :layer_three_network #, :required => true, :auto_validation => false
53
+
54
+ before :create, :create_layer_three_network
55
+ before :create, :set_private_address
56
+ after :create, :merge_layer_three_networks
57
+
58
+ # validates_format :address,
59
+ # :with => %r{\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}(?:\/[1-3]\d)?\z},
60
+ # :message => 'invalid IP network format'
61
+
62
+ # We're doing all the validations here manually
63
+ # (rather than using the shortcuts above) so they
64
+ # will be executed in order. If the shortcut methods
65
+ # are used, this validation is ran first so when
66
+ # an invalid address is given the @ip_net object
67
+ # doesn't exist and NoMethodErrors are thrown.
68
+ validates_with_block :address do
69
+ format = %r{\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}(?:\/[1-3]\d)?\z}
70
+ @ip_net ||= Antfarm::IPAddrExt.new(self.address) rescue nil
71
+
72
+ if self.address.nil?
73
+ [ false, 'must be present' ]
74
+ elsif not self.address =~ format
75
+ [ false, 'invalid IP network format' ]
76
+ elsif @ip_net.loopback_address?
77
+ [ false, 'loopback address not allowed' ]
78
+ else
79
+ true
80
+ end
81
+ end
82
+
83
+ #######
84
+ private
85
+ #######
86
+
87
+ def create_layer_three_network
88
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] IpNetwork#create_layer_three_network'
89
+
90
+ # Only create a new layer 3 network if
91
+ # a layer 3 network model isn't already
92
+ # associated with this model. This protects
93
+ # against new layer 3 networks being
94
+ # created when one is already provided or
95
+ # when this model is being saved rather
96
+ # than created (since a layer 3 network
97
+ # will be automatically created and associated
98
+ # with this model on creation).
99
+ self.layer_three_network ||= Antfarm::Model::LayerThreeNetwork.create
100
+ end
101
+
102
+ def set_private_address
103
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] IpNetwork#set_private_address'
104
+
105
+ @ip_net ||= Antfarm::IPAddrExt.new(self.address) rescue return nil
106
+ self.private = @ip_net.private_address?
107
+ # TODO: Create private network objects.
108
+ return # if we don't do this, then a false is returned and the save fails
109
+ end
110
+
111
+ def merge_layer_three_networks
112
+ # Merge any existing networks already in the database that are
113
+ # sub_networks of this new network.
114
+ LayerThreeNetwork.merge(self.layer_three_network, 0.80)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,88 @@
1
+ ################################################################################
2
+ # #
3
+ # Copyright (2008-2010) Sandia Corporation. Under the terms of Contract #
4
+ # DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains #
5
+ # certain rights in this software. #
6
+ # #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy #
8
+ # of this software and associated documentation files (the "Software"), to #
9
+ # deal in the Software without restriction, including without limitation the #
10
+ # rights to use, copy, modify, merge, publish, distribute, distribute with #
11
+ # modifications, sublicense, and/or sell copies of the Software, and to permit #
12
+ # persons to whom the Software is furnished to do so, subject to the following #
13
+ # conditions: #
14
+ # #
15
+ # The above copyright notice and this permission notice shall be included in #
16
+ # all copies or substantial portions of the Software. #
17
+ # #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
21
+ # ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #
22
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR #
23
+ # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE #
24
+ # SOFTWARE. #
25
+ # #
26
+ # Except as contained in this notice, the name(s) of the above copyright #
27
+ # holders shall not be used in advertising or otherwise to promote the sale, #
28
+ # use or other dealings in this Software without prior written authorization. #
29
+ # #
30
+ ################################################################################
31
+
32
+ module Antfarm
33
+ module Model
34
+ class LayerThreeInterface
35
+ include DataMapper::Resource
36
+
37
+ storage_names[:default] = 'layer_three_interfaces'
38
+
39
+ property :id, Serial
40
+ property :certainty_factor, Float, :required => true, :default => 0.8
41
+ property :protocol, String
42
+ property :custom, String
43
+
44
+ belongs_to :layer_two_interface, :required => true
45
+ belongs_to :layer_three_network, :required => true
46
+
47
+ validates_present :certainty_factor
48
+
49
+ # Need to do this before validation since
50
+ # :required => true is specified on the
51
+ # layer 2 interface association above.
52
+ #
53
+ # Note that we do NOT automatically create
54
+ # a layer 3 network on creation. That should
55
+ # ALWAYS be provided to this model by the
56
+ # IP interface model when it's being created.
57
+ before :valid?, :create_layer_two_interface
58
+ before :save, :clamp_certainty_factor
59
+
60
+ #######
61
+ private
62
+ #######
63
+
64
+ # If a hash is passed into the layer 2 interface variable,
65
+ # parameters matching variables on the layer 2 interface
66
+ # class will be used to create a new layer 2 interface object.
67
+ def create_layer_two_interface
68
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] LayerThreeInterface#create_layer_two_interface'
69
+
70
+ # Only create a new layer 2 interface if a
71
+ # layer 2 interface model isn't already
72
+ # associated with this model. This protects
73
+ # against new layer 2 interfaces being
74
+ # created when one is already provided or
75
+ # when this model is being saved rather
76
+ # than created (since a layer 2 interface
77
+ # will be automatically created and
78
+ # associated with this model on creation).
79
+ self.layer_two_interface ||= Antfarm::Model::LayerTwoInterface.create
80
+ end
81
+
82
+ def clamp_certainty_factor
83
+ Antfarm::Helpers.log :debug, '[PRIVATE METHOD CALLED] LayerThreeInterface#clamp_certainty_factor'
84
+ self.certainty_factor = Antfarm::Helpers.clamp(self.certainty_factor)
85
+ end
86
+ end
87
+ end
88
+ end