github.com/coreos/mantle@v0.13.0/network/ntp/_ntpd/ntpd.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/coreos/pkg/capnslog"
    23  
    24  	"github.com/coreos/mantle/network/ntp"
    25  )
    26  
    27  var (
    28  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "main")
    29  	now  = flag.String("now", "", "Internal time for the server.")
    30  	leap = flag.String("leap", "", "Handle a leap second.")
    31  )
    32  
    33  func main() {
    34  	flag.Parse()
    35  	capnslog.SetFormatter(capnslog.NewStringFormatter(os.Stderr))
    36  	capnslog.SetGlobalLogLevel(capnslog.INFO)
    37  
    38  	var l, n time.Time
    39  	var err error
    40  	if *now != "" {
    41  		n, err = time.Parse(time.UnixDate, *now)
    42  		if err != nil {
    43  			plog.Fatalf("Parsing --now failed: %v", err)
    44  		}
    45  	}
    46  	if *leap != "" {
    47  		l, err = time.Parse(time.UnixDate, *leap)
    48  		if err != nil {
    49  			plog.Fatalf("Parsing --leap failed: %v", err)
    50  		}
    51  		if (l.Truncate(24*time.Hour) != l) || (l.UTC().Day() != 1) {
    52  			plog.Fatalf("Invalid --leap time: %s", l)
    53  		}
    54  	}
    55  
    56  	s, err := ntp.NewServer(":123")
    57  	if err != nil {
    58  		plog.Fatalf("Listen failed: %v", err)
    59  	}
    60  
    61  	if !n.IsZero() {
    62  		s.SetTime(n)
    63  	}
    64  	if !l.IsZero() {
    65  		s.SetLeapSecond(l, ntp.LEAP_ADD)
    66  	}
    67  
    68  	s.Serve()
    69  }