virtuaservices 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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/lib/virtuaservices.rb +11 -0
  3. data/lib/virtuaservices/account.rb +72 -0
  4. data/lib/virtuaservices/authentication.rb +7 -0
  5. data/lib/virtuaservices/authentication/session.rb +28 -0
  6. data/lib/virtuaservices/concerns.rb +13 -0
  7. data/lib/virtuaservices/concerns/activable.rb +18 -0
  8. data/lib/virtuaservices/concerns/diagnosticable.rb +22 -0
  9. data/lib/virtuaservices/concerns/enumerable.rb +44 -0
  10. data/lib/virtuaservices/concerns/mime_typable.rb +36 -0
  11. data/lib/virtuaservices/concerns/premiumable.rb +15 -0
  12. data/lib/virtuaservices/concerns/sluggable.rb +27 -0
  13. data/lib/virtuaservices/concerns/typable.rb +17 -0
  14. data/lib/virtuaservices/monitoring.rb +12 -0
  15. data/lib/virtuaservices/monitoring/action.rb +25 -0
  16. data/lib/virtuaservices/monitoring/gateway.rb +37 -0
  17. data/lib/virtuaservices/monitoring/instance.rb +36 -0
  18. data/lib/virtuaservices/monitoring/route.rb +36 -0
  19. data/lib/virtuaservices/monitoring/service.rb +37 -0
  20. data/lib/virtuaservices/monitoring/websocket.rb +25 -0
  21. data/lib/virtuaservices/oauth.rb +7 -0
  22. data/lib/virtuaservices/oauth/application.rb +33 -0
  23. data/lib/virtuaservices/permissions.rb +10 -0
  24. data/lib/virtuaservices/permissions/category.rb +15 -0
  25. data/lib/virtuaservices/permissions/group.rb +30 -0
  26. data/lib/virtuaservices/permissions/right.rb +19 -0
  27. data/lib/virtuaservices/specs.rb +89 -0
  28. data/lib/virtuaservices/utils.rb +10 -0
  29. data/lib/virtuaservices/utils/controllers.rb +8 -0
  30. data/lib/virtuaservices/utils/controllers/base.rb +193 -0
  31. data/lib/virtuaservices/utils/controllers/checked.rb +14 -0
  32. data/lib/virtuaservices/utils/errors.rb +12 -0
  33. data/lib/virtuaservices/utils/errors/bad_request.rb +14 -0
  34. data/lib/virtuaservices/utils/errors/forbidden.rb +14 -0
  35. data/lib/virtuaservices/utils/errors/http_error.rb +30 -0
  36. data/lib/virtuaservices/utils/errors/not_found.rb +14 -0
  37. data/lib/virtuaservices/utils/loaders.rb +7 -0
  38. data/lib/virtuaservices/utils/loaders/heroku.rb +20 -0
  39. data/lib/virtuaservices/utils/micro_service.rb +170 -0
  40. data/lib/virtuaservices/utils/seeder.rb +25 -0
  41. data/lib/virtuaservices/version.rb +3 -0
  42. metadata +321 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 14c9250ccc5f41dec23c9cefe1be32a519667d8df724b037e5be4bee496c0faa
4
+ data.tar.gz: 4af97b4b7012372092b43c2741525baa7d7164a6d8c6ade9dc0cc1d9bf7f5052
5
+ SHA512:
6
+ metadata.gz: f735fdb67d12698228fc0c236d3749184012046161d2c71efae1ec3a43c169bb8aaf93c2098be49ec3cc7201ed0270db63c0ae099f74c7e63551fef82ccca489
7
+ data.tar.gz: 8affa59effed0aca1ad2f2c5df2969c543081afcd21cc2efcfa8613929b87ea3349d1616ff0c2094ebf65551e386c33f7efbf30681f55ae08de43d06604a1557
@@ -0,0 +1,11 @@
1
+ require 'mongoid'
2
+
3
+ module Virtuaservices
4
+ autoload :Account , 'virtuaservices/account'
5
+ autoload :Authentication, 'virtuaservices/authentication'
6
+ autoload :Concerns , 'virtuaservices/concerns'
7
+ autoload :Monitoring , 'virtuaservices/monitoring'
8
+ autoload :OAuth , 'virtuaservices/oauth'
9
+ autoload :Permissions , 'virtuaservices/permissions'
10
+ autoload :Utils , 'virtuaservices/utils'
11
+ end
@@ -0,0 +1,72 @@
1
+ module Virtuaservices
2
+ # A user account with all related attributes. It holds credentials and informations about a designated user.
3
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
4
+ class Account
5
+ include Mongoid::Document
6
+ include Mongoid::Timestamps
7
+ include ActiveModel::SecurePassword
8
+ include Virtuaservices::Concerns::Enumerable
9
+
10
+ # @!attribute [rw] username
11
+ # @return [String] the nickname the user chose at subscription, must be given, unique, and 6 or more characters long.
12
+ field :username, type: String
13
+ # @!attribute [r] password_digest
14
+ # @return [String] the password of the user, encrypted with the Blowfish algorithm.
15
+ field :password_digest, type: String
16
+ # @!attribute [rw] lastname
17
+ # @return [String] the last name (family name) of the user.
18
+ field :lastname, type: String, default: ''
19
+ # @!attribute [rw] firstname
20
+ # @return [String] the first name of the user.
21
+ field :firstname, type: String, default: ''
22
+ # @!attribute [rw] email
23
+ # @return [String] the email address of the user, useful to contact them ; it must be given, unique, and have an email format.
24
+ field :email, type: String
25
+ # @!attribute [rw] language
26
+ # @return [Symbol] the language preferred by this user.
27
+ enum_field :language, [:en_GB, :fr_FR], default: :fr_FR
28
+ # @!attribute [rw] gender
29
+ # @return [Symbol] the way you prefer the application to gender you.
30
+ enum_field :gender, [:female, :male, :neutral], default: :neutral
31
+
32
+ # @!attribute [w] password
33
+ # @return [String] password, in clear, of the user ; do not attempt to get the value, just set it when changing the password.
34
+ # @!attribute [w] password_confirmation
35
+ # @return [String] the confirmation of the password, do not get, just set it ; it must be the same as the password.
36
+ has_secure_password validations: false
37
+
38
+ # @!attribute [rw] groups
39
+ # @return [Array<Virtuaservices::Permissions::Group>] the groups giving their corresponding rights to the current account.
40
+ has_and_belongs_to_many :groups, class_name: 'Virtuaservices::Permissions::Group', inverse_of: :accounts
41
+
42
+ # @!attribute [rw] applications
43
+ # @return [Array<Virtuaservices::OAuth::Application] the applications this user has created and owns.
44
+ has_many :applications, class_name: 'Virtuaservices::OAuth::Application', inverse_of: :creator
45
+ # @!attribute [rw] services
46
+ # @return [Array<Virtuaservices::Monitoring::Service>] the services created by this user.
47
+ has_many :services, class_name: 'Virtuaservices::Monitoring::Service', inverse_of: :creator
48
+ # @!attribute [rw] sessions
49
+ # @return [Array<Virtuaservices::Authentication::Session>] the sessions on which this account is, or has been logged in.
50
+ has_many :sessions, class_name: 'Virtuaservices::Authentication::Session', inverse_of: :account
51
+ # @!attribute [rw] websockets
52
+ # @return [Array<Virtuaservices::Monitoring::Websocket>] the websockets created by the owner of this account.
53
+ has_many :websockets, class_name: 'Virtuaservices::Monitoring::Websocket', inverse_of: :creator
54
+
55
+ validates :username,
56
+ presence: {message: 'required'},
57
+ length: {minimum: 6, message: 'minlength', if: :username?},
58
+ uniqueness: {message: 'uniq', if: :username?}
59
+
60
+ validates :email,
61
+ presence: {message: 'required'},
62
+ format: {with: /\A[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}\z/, message: 'pattern', if: :email?},
63
+ uniqueness: {message: 'uniq', if: :email?}
64
+
65
+ validates :password,
66
+ presence: {message: 'required', if: ->{ !persisted? || password_digest_changed? }},
67
+ confirmation: {message: 'confirmation', if: :password_digest_changed?}
68
+
69
+ validates :password_confirmation,
70
+ presence: {message: 'required', if: :password_digest_changed?}
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ module Virtuaservices
2
+ # This module holds the logic for user authentication to our frontend.
3
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
4
+ module Authentication
5
+ autoload :Session, 'virtuaservices/authentication/session'
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ module Virtuaservices
2
+ module Authentication
3
+ # A session represents the connection of the user on our frontend application. Nobody else than our frontend should
4
+ # have access to the session or it's content (in particular to the token), instead they shall use the OAuth2.0 protocol.
5
+ # A session shall ONLY be created by a premium application (only our frontend applications are premium).
6
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
7
+ class Session
8
+ include Mongoid::Document
9
+ include Mongoid::Timestamps
10
+
11
+ # @!attribute [rw] token
12
+ # @return [String] the unique token for this session, used to identify it and be sure the user is connected on this application.
13
+ field :token, type: String
14
+ # @!attribute [rw] websocket_id
15
+ # @return [String] the ID of the websocket on which the session is connected. It's not an association because instances are embedded.
16
+ field :websocket_id, type: String, default: ''
17
+
18
+ # @!attribute [rw] account
19
+ # @return [Virtuaservices::Account] the account connected to the application.
20
+ belongs_to :account, class_name: 'Virtuaservices::Account', inverse_of: :sessions
21
+
22
+ validates :token,
23
+ presence: {message: 'required'},
24
+ uniqueness: {message: 'uniq', if: :token?},
25
+ length: {minimum: 10, message: 'minlength', if: :token?}
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Virtuaservices
2
+ # This module holds the shared concerns to include in the desired models.
3
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
4
+ module Concerns
5
+ autoload :Activable , 'virtuaservices/concerns/activable'
6
+ autoload :Diagnosticable, 'virtuaservices/concerns/diagnosticable'
7
+ autoload :Enumerable , 'virtuaservices/concerns/enumerable'
8
+ autoload :MimeTypable , 'virtuaservices/concerns/mime_typable'
9
+ autoload :Premiumable , 'virtuaservices/concerns/premiumable'
10
+ autoload :Sluggable , 'virtuaservices/concerns/sluggable'
11
+ autoload :Typable , 'virtuaservices/concerns/typable'
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Concerns for the objects that can be activated or deactivated, included the corresponding scopes.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Activable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ # @!attribute [rw] active
10
+ # @return [Boolean] the active status of the instance, indicating if someone has deactivated it or not.
11
+ field :active, type: Boolean, default: true
12
+
13
+ scope :active , ->{ where(active: true) }
14
+ scope :inactive, ->{ where(active: false) }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Includes the diagnostic URL field, and the related validations.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Diagnosticable
6
+ extend ActiveSupport::Concern
7
+
8
+ # Module holding the class methods for the classes including this concern.
9
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
10
+ included do
11
+ # @!attribute [rw] diagnostic
12
+ # @return [String] the diagnostic URL to know the status of an entity (usually a gateway, or an instance of a service).
13
+ field :diagnostic, type: String, default: '/status'
14
+
15
+ validates :diagnostic,
16
+ presence: {message: "required"},
17
+ length: {minimum: 4, message: "minlength"},
18
+ format: {with: /\A(\/[a-z]+)+\z/, message: "pattern"}
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,44 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Defines enumerations for the Mongoid models. An enumeration is a field that can only use a given set of values.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Enumerable
6
+ extend ActiveSupport::Concern
7
+
8
+ # Submodule holding all the static methods add to the current subclass.
9
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
10
+ module ClassMethods
11
+
12
+ # Creates the field with the given name, set of possible values, and options.
13
+ # @param field_name [String] the name of the enumerated field.
14
+ # @param values [Array<Symbol>] the possible values of the enumerated field.
15
+ # @param options [Hash<Symbol, Any>] the possible options for the field.
16
+ def enum_field(field_name, values, options = {})
17
+ field :"enum_#{field_name}", type: Symbol, default: options[:default]
18
+
19
+ validates :"enum_#{field_name}", inclusion: {in: values.map(&:to_sym), message: 'inclusion'}
20
+
21
+ define_method field_name do
22
+ return self["enum_#{field_name}"]
23
+ end
24
+
25
+ define_method "#{field_name}=" do |value|
26
+ if values.include? value.to_sym
27
+ self["enum_#{field_name}"] = value.to_sym
28
+ end
29
+ end
30
+
31
+ values.map(&:to_sym).each do |value|
32
+ define_method "#{field_name}_#{value}!" do
33
+ self["enum_#{field_name}"] = value
34
+ end
35
+
36
+ define_method "#{field_name}_#{value}?" do
37
+ self["enum_#{field_name}"] == value
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Includes the MIME type field to files to ensure only supported types are included.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module MimeTypable
6
+ extend ActiveSupport::Concern
7
+
8
+ # Submodule holding all the static methods add to the current subclass.
9
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
10
+ module ClassMethods
11
+ # Defines the MIME type attribute with the given possible MIME types.
12
+ # @param values [Array<String>] the possible MIME types, * can be used as wild cards.
13
+ def mime_type(values)
14
+
15
+ # @!attribute [rw] mime_type
16
+ # @return [String] the MIME type of the file, obtained from the uploaded file.
17
+ field :mime_type, type: String
18
+
19
+ validates :mime_type, presence: {message: 'required'}
20
+
21
+ validate :mime_type_validity, if: :mime_type?
22
+
23
+ # Validates the validity of the MIME type by checking if it respects any of the given mime types.
24
+ # If it does not respect any MIME types possible, it adds an error to the mime_type field and invalidates.
25
+ define_method :mime_type_validity do
26
+ values.each do |type|
27
+ type_regex = ::Regexp.new("^#{type.sub(/\*/, '(.+)')}$")
28
+ return true if !type_regex.match(mime_type).nil?
29
+ end
30
+ errors.add(:mime_type, 'pattern')
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Includes the premium field to make the entity including it accessible only to premium applications or not.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Premiumable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ # @!attribute [rw] premium
10
+ # @return [Boolean] TRUE if the entity is made to be accessible only to premiuma pplications, FALSE otherwise.
11
+ field :premium, type: Boolean, default: false
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Includes the slug field, always the same in all models.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Sluggable
6
+ extend ActiveSupport::Concern
7
+
8
+ # Module holding the class methods for the classes including this concern.
9
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
10
+ module ClassMethods
11
+ # Add the field and its validations in the model including it.
12
+ # @param entity_type [String,Symbol] the name of the model including it, to be included in the error messages.
13
+ def make_sluggable(entity_type)
14
+ # @!attribute [rw] slug
15
+ # @return [String] the slug of the current entity ; it must be snake-cased, longer than four characters, unique for the entity and given.
16
+ field :slug, type: String
17
+
18
+ validates :slug,
19
+ length: {minimum: 4, message: 'minlength', if: :slug?},
20
+ format: {with: /\A[a-z]+(_[a-z]+)*\z/, message: 'pattern', if: :slug?},
21
+ uniqueness: {message: 'uniq', if: :slug?},
22
+ presence: {message: 'required'}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ module Virtuaservices
2
+ module Concerns
3
+ # Concerns for the objects that can be activated or deactivated, included the corresponding scopes.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Typable
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include Virtuaservices::Concerns::Enumerable
10
+
11
+ # @!attribute [rw] type
12
+ # @return [Symbol] the type of the instance, determining its way of being deployed, restarted, etc.
13
+ enum_field :type, [:heroku, :local, :unix], default: :heroku
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module Virtuaservices
2
+ # The monitoring module holds all the logic about the services so they can be activated or deactivated.
3
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
4
+ module Monitoring
5
+ autoload :Action , 'virtuaservices/monitoring/action'
6
+ autoload :Gateway , 'virtuaservices/monitoring/gateway'
7
+ autoload :Instance , 'virtuaservices/monitoring/instance'
8
+ autoload :Route , 'virtuaservices/monitoring/route'
9
+ autoload :Service , 'virtuaservices/monitoring/service'
10
+ autoload :Websocket, 'virtuaservices/monitoring/websocket'
11
+ end
12
+ end
@@ -0,0 +1,25 @@
1
+ module Virtuaservices
2
+ module Monitoring
3
+ # An action is made by an authorized user on the instance of a server to perform a task.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ class Action
6
+ include Mongoid::Document
7
+ include Mongoid::Timestamps
8
+ include Virtuaservices::Concerns::Enumerable
9
+
10
+ # @!attribute [rw] type
11
+ # @return [Symbol] the type of action you're making on this instance
12
+ enum_field :type, [:restart]
13
+ # @!attribute [rw] success
14
+ # @return [Boolean] TRUE if the action succeeded (or at least was successfully launched), FALSE otherwise.
15
+ field :success, type: Boolean, default: false
16
+
17
+ # @!attribute [rw] user
18
+ # @return [Virtuaservices::Account] the user performing the action on the instance.
19
+ belongs_to :user, class_name: 'Virtuaservices::Account'
20
+ # @!attribute [rw] instance
21
+ # @return [Virtuaservices::Monitoring::Instance] the instance of a service on which the action is performed.
22
+ embedded_in :instance, class_name: 'Virtuaservices::Monitoring::Instance', inverse_of: :actions
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ module Virtuaservices
2
+ module Monitoring
3
+ # A gateway is a portal by which you access the different web services of the application suite.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ class Gateway
6
+ include Mongoid::Document
7
+ include Mongoid::Timestamps
8
+ include Virtuaservices::Concerns::Activable
9
+ include Virtuaservices::Concerns::Diagnosticable
10
+ include Virtuaservices::Concerns::Typable
11
+
12
+ # @!attribute [rw] url
13
+ # @return [String] the URL of the gateway, where the requests will be issued.
14
+ field :url, type: String
15
+ # @!attribute [rw] running
16
+ # @return [Boolean] the running status of the gateway, indicating if it can be used or not.
17
+ field :running, type: Boolean, default: false
18
+ # @!attribute [rw] gateways
19
+ # @return [String] the uniq token for this gateway, identifying it in the micro services.
20
+ field :token, type: String
21
+
22
+ scope :running , ->{ where(running: true) }
23
+
24
+ validates :url,
25
+ presence: {message: 'required'},
26
+ format: {
27
+ with: /\A(https?:\/\/)((localhost:[0-9]+)|(([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*)|(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b):([0-9]{2,6}))\/?\z/,
28
+ message: 'pattern',
29
+ if: :url?
30
+ }
31
+
32
+ validates :token,
33
+ presence: {message: 'required'},
34
+ uniqueness: {message: 'uniq'}
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ module Virtuaservices
2
+ module Monitoring
3
+ # An instance is one of the services, deployed on one server. A service may have many instances to balance the load between them all.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ class Instance
6
+ include Mongoid::Document
7
+ include Mongoid::Timestamps
8
+ include Virtuaservices::Concerns::Activable
9
+ include Virtuaservices::Concerns::Typable
10
+
11
+ # @!attribute [rw] url
12
+ # @return [String] the URL of the instance, where the requests will be issued.
13
+ field :url, type: String
14
+ # @!attribute [rw] running
15
+ # @return [Boolean] the running status of the instance, indicating if it can be used or not.
16
+ field :running, type: Boolean, default: false
17
+ # @!attribute [rw] data
18
+ # @return [Hash] the additional datas for this instance (for example for an Heroku instance it's all the data provided by the API)
19
+ field :data, type: Hash, default: {}
20
+
21
+ scope :running , ->{ where(running: true) }
22
+
23
+ # @!attribute [r] service
24
+ # @return [Virtuaservices::Monitoring::Service] the service this instance is linked to.
25
+ embedded_in :service, class_name: 'Virtuaservices::Monitoring::Service', inverse_of: :instances
26
+ # @!attribute [rw] actions
27
+ # @return [Virtuaservices::Monitoring::Action] the actions that has been performed on the service.
28
+ embeds_many :actions, class_name: 'Virtuaservices::Monitoring::Action', inverse_of: :instance
29
+
30
+ validates :url,
31
+ presence: {message: 'required'},
32
+ format: {with: /\A(https?:\/\/)((([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*)|(localhost:[0-9]{2,4})\/?)\z/, message: 'pattern', if: :url?}
33
+ end
34
+ end
35
+ end
36
+