Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2854 Add timezone flag to start and a timezone command
- Loading branch information
1 parent
d264a7e
commit 8a1411b
Showing
10 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/minishift/minishift/pkg/minishift/timezone" | ||
"time" | ||
|
||
"github.com/docker/machine/libmachine" | ||
"github.com/minishift/minishift/cmd/minishift/cmd/util" | ||
"github.com/minishift/minishift/cmd/minishift/state" | ||
"github.com/minishift/minishift/pkg/minikube/constants" | ||
"github.com/minishift/minishift/pkg/util/os/atexit" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// stopCmd represents the stop command | ||
var ( | ||
timezoneCmd = &cobra.Command{ | ||
Use: "timezone", | ||
Short: "Set timezone for running Minishift Instance", | ||
Long: `Set timezone for running Minishift Instance, provided timezone should be valid.`, | ||
Run: runTimezone, | ||
Hidden: true, | ||
} | ||
set string | ||
list bool | ||
) | ||
|
||
func runTimezone(cmd *cobra.Command, args []string) { | ||
api := libmachine.NewClient(state.InstanceDirs.Home, state.InstanceDirs.Certs) | ||
defer api.Close() | ||
|
||
// if VM does not exist, exit with error | ||
util.ExitIfUndefined(api, constants.MachineName) | ||
|
||
hostVm, err := api.Load(constants.MachineName) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} | ||
|
||
if list { | ||
out, err := timezone.GetTimeZoneList(hostVm) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} | ||
fmt.Println(out) | ||
return | ||
} | ||
|
||
if set == "" { | ||
out, err := timezone.GetTimeZone(hostVm) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} | ||
fmt.Println(out) | ||
return | ||
} | ||
_, err = time.LoadLocation(set) | ||
if err != nil { | ||
atexit.ExitWithMessage(1, fmt.Sprintf("%s is not a vaild timezone: %s", set, err.Error())) | ||
} | ||
|
||
fmt.Printf("Setting timezone for Instance ... ") | ||
if err := timezone.UpdateTimeZoneInConfig(set); err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} | ||
if err := timezone.SetTimeZone(hostVm); err != nil { | ||
atexit.ExitWithMessage(1, err.Error()) | ||
} | ||
fmt.Println("OK") | ||
} | ||
|
||
func init() { | ||
timezoneCmd.Flags().StringVarP(&set, "set", "s", "", "Set the provided timezone.") | ||
timezoneCmd.Flags().BoolVarP(&list, "list", "l", false, "List of the available timezone to Minishift Instance") | ||
RootCmd.AddCommand(timezoneCmd) | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright (C) 2018 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package timezone | ||
|
||
import ( | ||
"fmt" | ||
"github.com/docker/machine/libmachine/host" | ||
"github.com/docker/machine/libmachine/provision" | ||
minishiftConfig "github.com/minishift/minishift/pkg/minishift/config" | ||
minishiftConstants "github.com/minishift/minishift/pkg/minishift/constants" | ||
) | ||
|
||
func SetTimeZone(host *host.Host) error { | ||
if minishiftConfig.InstanceStateConfig != nil { | ||
cmd := fmt.Sprintf("sudo timedatectl set-timezone '%s'", minishiftConfig.InstanceStateConfig.TimeZone) | ||
sshCommander := provision.GenericSSHCommander{Driver: host.Driver} | ||
if _, err := sshCommander.SSHCommand(cmd); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func UpdateTimeZoneInConfig(timezone string) error { | ||
if minishiftConfig.InstanceStateConfig == nil { | ||
minishiftConfig.InstanceStateConfig, _ = minishiftConfig.NewInstanceStateConfig(minishiftConstants.GetInstanceStateConfigPath()) | ||
} | ||
minishiftConfig.InstanceStateConfig.TimeZone = timezone | ||
return minishiftConfig.InstanceStateConfig.Write() | ||
} | ||
|
||
func GetTimeZone(host *host.Host) (string, error) { | ||
cmd := "sudo timedatectl status" | ||
sshCommander := provision.GenericSSHCommander{Driver: host.Driver} | ||
output, err := sshCommander.SSHCommand(cmd) | ||
if err != nil { | ||
return "", err | ||
} | ||
return output, nil | ||
} | ||
|
||
func GetTimeZoneList(host *host.Host) (string, error) { | ||
cmd := "sudo timedatectl list-timezones" | ||
sshCommander := provision.GenericSSHCommander{Driver: host.Driver} | ||
output, err := sshCommander.SSHCommand(cmd) | ||
if err != nil { | ||
return "", err | ||
} | ||
return output, nil | ||
} |
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