Last active
August 3, 2022 20:26
-
-
Save jitingcn/668e52c692b893d02a530b022b912878 to your computer and use it in GitHub Desktop.
auto get a desired ip range for aws-lightsail-jp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk-lightsail' | |
ls = Aws::Lightsail::Client.new(region: "ap-northeast-1") | |
pattern = /^(103|175|46)/ | |
instance_name = "vm" | |
ip_pool = Set.new | |
ip_names = 4.times.map { |i| "vm-ip-#{i}" } | |
loop do | |
ip_names.each { |ip_name| ls.allocate_static_ip(static_ip_name: ip_name) } | |
found = false | |
ip_names.each do |ip_name| | |
ip = ls.get_static_ip(static_ip_name: ip_name).static_ip.ip_address | |
ip_pool.add(ip) | |
puts ip | |
if ip.match?(pattern) | |
found = true | |
ls.attach_static_ip(static_ip_name: ip_name, instance_name: instance_name) | |
(ip_names - [ip_name]).each { |x| ls.release_static_ip(static_ip_name: x) } | |
break | |
end | |
end | |
break if found | |
ip_names.each { |ip_name| ls.release_static_ip(static_ip_name: ip_name) } | |
puts "pass ip count #{ip_pool.size}" | |
puts "not found, wait 30s" | |
sleep(30) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment