homebase 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69f312ad0d13dc58a0556e73f83eb6136022fbda
4
+ data.tar.gz: 67a97e3c2acf7df9870d5128feca34d839a4d912
5
+ SHA512:
6
+ metadata.gz: fd5a6939b138190eaf381f619ff6782332aa54f8f29b90d714bb51314073a0f7c62698c2e78dac82de9d048de60fa545a8dfc32b7129d53148b05dc960677d2e
7
+ data.tar.gz: 138f01e761458acb805a5a8e11b8c4cfad3f02fa08516dbfa092c64605e0fda788af16068aeeeef7e05cf5883439749086a6cfac50c0381ef8d29d22fcfae0d5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ AllCops:
2
+ Include:
3
+ - Gemfile
4
+ - Rakefile
5
+ - '**/*.rb'
6
+ - homebase.gemspec
7
+ - bin/homebase
8
+ Metrics/AbcSize:
9
+ Enabled: no
10
+ Metrics/CyclomaticComplexity:
11
+ Enabled: no
12
+ Metrics/MethodLength:
13
+ Enabled: no
14
+ Metrics/PerceivedComplexity:
15
+ Enabled: no
16
+ Style/CaseIndentation:
17
+ Enabled: no
18
+ Style/Documentation:
19
+ Enabled: no
20
+ Style/GuardClause:
21
+ Enabled: no
22
+ Style/LineEndConcatenation:
23
+ Enabled: no
24
+ Style/NumericLiterals:
25
+ Enabled: no
26
+ Style/PercentLiteralDelimiters:
27
+ Enabled: no
28
+ Style/RegexpLiteral:
29
+ Enabled: no
30
+ Style/RedundantSelf:
31
+ Enabled: no
32
+ Style/SignalException:
33
+ Enabled: no
34
+ Style/SpaceInsideHashLiteralBraces:
35
+ Enabled: no
36
+
37
+
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at bsdavidson@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in castle.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Brian Davidson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Homebase
2
+
3
+ Homebase grabs your current public IP address and updates a Digital Ocean hostname
4
+ to point to the IP. You can use this to provide your own dynamic DNS for home
5
+ computers.
6
+
7
+ ## Installation
8
+
9
+ Install it yourself with:
10
+
11
+ $ gem install homebase
12
+
13
+ ## Usage
14
+
15
+ Run from commandline:
16
+
17
+ $ homebase --domain example.com --record subdomain --token YOUR_API_TOKEN
18
+
19
+ Your domain will need to be hosted with Digital Ocean and you'll need a
20
+ [Digital Ocean API token](https://cloud.digitalocean.com/settings/api/tokens).
21
+
22
+ ## Development
23
+
24
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
+
26
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+
28
+ ## Contributing
29
+
30
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/homebase. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
31
+
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
+
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError => e
7
+ STDERR.puts "Error importing RSpec: #{e}"
8
+ end
9
+
10
+ begin
11
+ require 'rubocop/rake_task'
12
+ RuboCop::RakeTask.new
13
+ rescue LoadError => e
14
+ STDERR.puts "Error importing RuboCop: #{e}"
15
+ end
16
+
17
+ task default: [:spec, :rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'homebase'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/homebase ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'homebase'
4
+ require 'trollop'
5
+
6
+ BANNER = %(
7
+ #{DATA}
8
+
9
+ Usage: #{File.basename $PROGRAM_NAME} [options]
10
+
11
+ Lookup your current public IP and update a Digital Ocean domain record with your
12
+ new address.
13
+
14
+ Options:
15
+ )
16
+
17
+ opts = Trollop.options do
18
+ banner BANNER
19
+ opt :domain, 'Domain name hosted with Digital Ocean', type: :string,
20
+ required: true
21
+ opt :record, 'Subdomain to update', type: :string, required: true
22
+ opt :token, 'Digital Ocean API token', type: :string, required: true
23
+ end
24
+
25
+ begin
26
+ homebase = Homebase.new
27
+ result = homebase.update_record_with_public_ip(opts[:domain],
28
+ opts[:record],
29
+ opts[:token])
30
+ puts result[:ip]
31
+ rescue => e
32
+ STDERR.puts "Error: #{e}"
33
+ exit 1
34
+ end
35
+
36
+ __END__
37
+
38
+ _ _ ____ __ __ ______ ____ _____ ______
39
+ | | | |/ __ \| \/ | ____| _ \ /\ / ____| ____| o____
40
+ | |__| | | | | \ / | |__ | |_) | / \ | (___ | |__ |\\//|
41
+ | __ | | | | |\/| | __| | _ < / /\ \ \___ \| __| |//\\|
42
+ | | | | |__| | | | | |____| |_) / ____ \ ____) | |____ |
43
+ |_| |_|\____/|_| |_|______|____/_/ \_\_____/|______| |
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/homebase.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'homebase/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'homebase'
8
+ spec.version = Homebase::VERSION
9
+ spec.authors = ['Brian Davidson']
10
+ spec.email = ['bsdavidson@gmail.com']
11
+
12
+ spec.summary = 'Host your own dynamic DNS via Digital Ocean'
13
+ spec.homepage = 'https://github.com/bsdavidson/homebase-ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'bin'
20
+ spec.executables = ['homebase']
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'trollop', '~> 2.1'
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.4'
27
+ spec.add_development_dependency 'simplecov', '~> 0.11'
28
+ spec.add_development_dependency 'rubocop', '~> 0.35'
29
+ end
@@ -0,0 +1,3 @@
1
+ class Homebase
2
+ VERSION = '0.1.0'
3
+ end
data/lib/homebase.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'json'
2
+ require 'ipaddr'
3
+ require 'net/http'
4
+ require_relative 'homebase/version'
5
+
6
+ class Homebase
7
+ API_PREFIX = 'https://api.digitalocean.com/v2'
8
+ PUBLIC_IP_URI = URI('https://api.ipify.org/?format=text')
9
+ DOMAIN_RECORDS_FORMAT = "#{API_PREFIX}/domains/%<domain_name>s/records"
10
+ DOMAIN_RECORD_FORMAT =
11
+ "#{API_PREFIX}/domains/%<domain_name>s/records/%<record_id>d"
12
+
13
+ class Error < StandardError; end
14
+ class RequestError < Error; end
15
+ class ParseError < Error; end
16
+
17
+ def public_ip(opts = {})
18
+ return @public_ip if @public_ip && !opts[:force]
19
+ res = Net::HTTP.get_response(PUBLIC_IP_URI)
20
+ raise(RequestError, "Invalid IP response #{res.code}",
21
+ caller) if res.code != '200'
22
+ ip = res.body.strip
23
+ begin
24
+ IPAddr.new(ip)
25
+ rescue IPAddr::InvalidAddressError
26
+ raise ParseError, 'Invalid Public IP', caller
27
+ end
28
+ @public_ip = ip
29
+ end
30
+
31
+ def get_record_by_name(domain_name, record_name, token)
32
+ uri = URI(DOMAIN_RECORDS_FORMAT % {domain_name: domain_name})
33
+
34
+ req = Net::HTTP::Get.new(uri)
35
+ req['Content-Type'] = 'application/json'
36
+ req['Authorization'] = "Bearer #{token}"
37
+ body = Net::HTTP.start(uri.host, uri.port,
38
+ use_ssl: uri.scheme == 'https') do |http|
39
+ res = http.request req
40
+ raise RequestError, "Error - #{res.code}", caller if res.code != '200'
41
+ res.body
42
+ end
43
+ begin
44
+ records = JSON.parse(body, symbolize_names: true)[:domain_records]
45
+ rescue JSON::ParserError
46
+ raise ParseError, 'Invalid JSON Response', caller
47
+ end
48
+ records.find { |record| record[:name] == record_name }
49
+ end
50
+
51
+ def update_record(domain_name, record_id, data, token)
52
+ uri = URI(DOMAIN_RECORD_FORMAT % {domain_name: domain_name,
53
+ record_id: record_id})
54
+ req = Net::HTTP::Put.new(uri)
55
+ req['Content-Type'] = 'application/json'
56
+ req['Authorization'] = "Bearer #{token}"
57
+ req.body = { data: data }.to_json
58
+ Net::HTTP.start(uri.host, uri.port,
59
+ use_ssl: uri.scheme == 'https') do |http|
60
+ res = http.request req
61
+ raise RequestError, "Error - #{res.code}", caller if res.code != '200'
62
+ res
63
+ end
64
+ end
65
+
66
+ def update_record_with_public_ip(domain_name, record_name, token)
67
+ record = get_record_by_name(domain_name, record_name, token)
68
+ result = update_record(domain_name, record[:id], public_ip, token)
69
+ { result: result, ip: public_ip }
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: homebase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Davidson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: trollop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.11'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.11'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.35'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.35'
97
+ description:
98
+ email:
99
+ - bsdavidson@gmail.com
100
+ executables:
101
+ - homebase
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rubocop.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/homebase
114
+ - bin/setup
115
+ - homebase.gemspec
116
+ - lib/homebase.rb
117
+ - lib/homebase/version.rb
118
+ homepage: https://github.com/bsdavidson/homebase-ruby
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Host your own dynamic DNS via Digital Ocean
142
+ test_files: []
143
+ has_rdoc: