diff --git a/lib/linux_admin.rb b/lib/linux_admin.rb index 0c78169..40b4b4c 100644 --- a/lib/linux_admin.rb +++ b/lib/linux_admin.rb @@ -28,6 +28,7 @@ require 'linux_admin/physical_volume' require 'linux_admin/volume_group' require 'linux_admin/scap' +require 'linux_admin/time_date' module LinuxAdmin extend Common diff --git a/lib/linux_admin/time_date.rb b/lib/linux_admin/time_date.rb new file mode 100644 index 0000000..487d46b --- /dev/null +++ b/lib/linux_admin/time_date.rb @@ -0,0 +1,31 @@ +module LinuxAdmin + class TimeDate + extend Common + COMMAND = 'timedatectl' + + TimeCommandError = Class.new(StandardError) + + def self.system_timezone_detailed + result = run(cmd(COMMAND), :params => ["status"]) + result.output.split("\n").each do |l| + return l.split(':')[1].strip if l =~ /Time.*zone/ + end + end + + def self.system_timezone + system_timezone_detailed.split[0] + end + + def self.system_time=(time) + run!(cmd(COMMAND), :params => ["set-time", "#{time.strftime("%F %T")}", :adjust_system_clock]) + rescue AwesomeSpawn::CommandResultError => e + raise TimeCommandError, e.message + end + + def self.system_timezone=(zone) + run!(cmd(COMMAND), :params => ["set-timezone", zone]) + rescue AwesomeSpawn::CommandResultError => e + raise TimeCommandError, e.message + end + end +end diff --git a/spec/data/time_date/timedatectl_output b/spec/data/time_date/timedatectl_output new file mode 100644 index 0000000..34f09e2 --- /dev/null +++ b/spec/data/time_date/timedatectl_output @@ -0,0 +1,14 @@ + Local time: Thu 2015-09-24 17:54:02 EDT + Universal time: Thu 2015-09-24 21:54:02 UTC + RTC time: Thu 2015-09-24 21:54:02 + Time zone: America/New_York (EDT, -0400) + NTP enabled: yes +NTP synchronized: yes + RTC in local TZ: no + DST active: yes + Last DST change: DST began at + Sun 2015-03-08 01:59:59 EST + Sun 2015-03-08 03:00:00 EDT + Next DST change: DST ends (the clock jumps one hour backwards) at + Sun 2015-11-01 01:59:59 EDT + Sun 2015-11-01 01:00:00 EST diff --git a/spec/time_date_spec.rb b/spec/time_date_spec.rb new file mode 100644 index 0000000..f8d39f7 --- /dev/null +++ b/spec/time_date_spec.rb @@ -0,0 +1,74 @@ +describe LinuxAdmin::TimeDate do + RUN_COMMAND = described_class.cmd("timedatectl") + + def timedatectl_result + output = File.read(Pathname.new(data_file_path("time_date/timedatectl_output"))) + AwesomeSpawn::CommandResult.new("", output, "", 0) + end + + describe ".system_timezone_detailed" do + it "returns the correct timezone" do + awesome_spawn_args = [ + RUN_COMMAND, + :params => ["status"] + ] + expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result) + tz = described_class.system_timezone_detailed + expect(tz).to eq("America/New_York (EDT, -0400)") + end + end + + describe ".system_timezone" do + it "returns the correct timezone" do + awesome_spawn_args = [ + RUN_COMMAND, + :params => ["status"] + ] + expect(AwesomeSpawn).to receive(:run).with(*awesome_spawn_args).and_return(timedatectl_result) + tz = described_class.system_timezone + expect(tz).to eq("America/New_York") + end + end + + describe ".system_time=" do + it "sets the time" do + time = Time.new(2015, 1, 1, 1, 1, 1) + awesome_spawn_args = [ + RUN_COMMAND, + :params => ["set-time", "2015-01-01 01:01:01", :adjust_system_clock] + ] + expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args) + described_class.system_time = time + end + + it "raises when the command fails" do + time = Time.new(2015, 1, 1, 1, 1, 1) + err = AwesomeSpawn::CommandResultError.new("message", nil) + allow(AwesomeSpawn).to receive(:run!).and_raise(err) + expect do + described_class.send(:system_time=, time) + end.to raise_error(described_class::TimeCommandError, "message") + end + end + + describe ".system_timezone=" do + it "sets the timezone" do + zone = "Location/City" + awesome_spawn_args = [ + RUN_COMMAND, + :params => ["set-timezone", zone] + ] + expect(AwesomeSpawn).to receive(:run!).with(*awesome_spawn_args) + described_class.system_timezone = zone + end + + it "raises when the command fails" do + zone = "Location/City" + err = AwesomeSpawn::CommandResultError.new("message", nil) + allow(AwesomeSpawn).to receive(:run!).and_raise(err) + expect do + described_class.send(:system_timezone=, zone) + end.to raise_error(described_class::TimeCommandError, "message") + end + end +end