44package platform
55
66import (
7+ "fmt"
8+ "os/exec"
9+ "strconv"
10+ "strings"
711 "time"
12+
13+ "github.com/Azure/azure-container-networking/log"
814)
915
1016const (
@@ -32,8 +38,60 @@ func GetOSInfo() string {
3238
3339// GetLastRebootTime returns the last time the system rebooted.
3440func GetLastRebootTime () (time.Time , error ) {
35- var rebootTime time.Time
36- return rebootTime , nil
41+ var systemBootTime string
42+ out , err := exec .Command ("cmd" , "/c" , "systeminfo" ).Output ()
43+ if err != nil {
44+ log .Printf ("Failed to query systeminfo, err: %v" , err )
45+ return time.Time {}.UTC (), err
46+ }
47+
48+ systemInfo := strings .Split (string (out ), "\n " )
49+ for _ , systemProperty := range systemInfo {
50+ if strings .Contains (systemProperty , "Boot Time" ) {
51+ systemBootTime = strings .TrimSpace (strings .Split (systemProperty , "System Boot Time:" )[1 ])
52+ }
53+ }
54+
55+ if len (strings .TrimSpace (systemBootTime )) == 0 {
56+ log .Printf ("Failed to retrieve boot time from systeminfo" )
57+ return time.Time {}.UTC (), fmt .Errorf ("Failed to retrieve boot time from systeminfo" )
58+ }
59+
60+ log .Printf ("Boot time: %s" , systemBootTime )
61+ // The System Boot Time is in the following format "01/02/2006, 03:04:05 PM"
62+ // Formulate the Boot Time in the format: "2006-01-02 15:04:05"
63+ bootDate := strings .Split (systemBootTime , " " )[0 ]
64+ bootTime := strings .Split (systemBootTime , " " )[1 ]
65+ bootPM := strings .Contains (strings .Split (systemBootTime , " " )[2 ], "PM" )
66+
67+ month := strings .Split (bootDate , "/" )[0 ]
68+ day := strings .Split (bootDate , "/" )[1 ]
69+ year := strings .Split (bootDate , "/" )[2 ]
70+ year = strings .Trim (year , "," )
71+ hour := strings .Split (bootTime , ":" )[0 ]
72+ hourInt , _ := strconv .Atoi (hour )
73+ min := strings .Split (bootTime , ":" )[1 ]
74+ sec := strings .Split (bootTime , ":" )[2 ]
75+
76+ if bootPM && hourInt < 12 {
77+ hourInt += 12
78+ } else if ! bootPM && hourInt == 12 {
79+ hourInt = 0
80+ }
81+
82+ hour = strconv .Itoa (hourInt )
83+ systemBootTime = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
84+ log .Printf ("Formatted Boot time: %s" , systemBootTime )
85+
86+ // Parse the boot time.
87+ layout := "2006-01-02 15:04:05"
88+ rebootTime , err := time .ParseInLocation (layout , systemBootTime , time .Local )
89+ if err != nil {
90+ log .Printf ("Failed to parse boot time, err:%v" , err )
91+ return time.Time {}.UTC (), err
92+ }
93+
94+ return rebootTime .UTC (), nil
3795}
3896
3997func ExecuteCommand (command string ) (string , error ) {
@@ -43,3 +101,18 @@ func ExecuteCommand(command string) (string, error) {
43101func SetOutboundSNAT (subnet string ) error {
44102 return nil
45103}
104+
105+ // ClearNetworkConfiguration clears the azure-vnet.json contents.
106+ // This will be called only when reboot is detected - This is windows specific
107+ func ClearNetworkConfiguration () (bool , error ) {
108+ jsonStore := CNIRuntimePath + "azure-vnet.json"
109+ log .Printf ("Deleting the json store %s" , jsonStore )
110+ cmd := exec .Command ("cmd" , "/c" , "del" , jsonStore )
111+
112+ if err := cmd .Run (); err != nil {
113+ log .Printf ("Error deleting the json store %s" , jsonStore )
114+ return true , err
115+ }
116+
117+ return true , nil
118+ }
0 commit comments