<commit_before>package main

import (
	"os"
	"os/signal"
	"syscall"

	"github.com/coreos/go-iptables/iptables"
	"github.com/spf13/cobra"
	coreapi "k8s.io/api/core/v1"
	networkingv1 "k8s.io/api/networking/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/cache"

	"github.com/weaveworks/weave/common"
	"github.com/weaveworks/weave/net"
	"github.com/weaveworks/weave/net/ipset"
	"github.com/weaveworks/weave/npc"
	"github.com/weaveworks/weave/npc/metrics"
	"github.com/weaveworks/weave/npc/ulogd"
)

var (
	version        = "unreleased"
	metricsAddr    string
	logLevel       string
	allowMcast     bool
	nodeName       string
	maxList        int
	bridgePortName string
)

func handleError(err error) { common.CheckFatal(err) }

func makeController(getter cache.Getter, resource string,
	objType runtime.Object, handlers cache.ResourceEventHandlerFuncs) cache.Controller {
	listWatch := cache.NewListWatchFromClient(getter, resource, "", fields.Everything())
	_, controller := cache.NewInformer(listWatch, objType, 0, handlers)
	return controller
}

func resetIPTables(ipt *iptables.IPTables) error {
	// Flush chains first so there are no refs to extant ipsets
	if err := ipt.ClearChain(npc.TableFilter, npc.IngressChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.DefaultChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.MainChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressMarkChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressCustomChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressDefaultChain); err != nil {
		return err
	}

	// We do not clear npc.EgressChain here because otherwise, in the case of restarting
	// weave-npc process, all egress traffic is allowed for a short period of time.
	// The chain is created in createBaseRules.

	return nil
}

func resetIPSets(ips ipset.Interface) error {
	// Remove ipsets prefixed `weave-` only.

	sets, err := ips.List(npc.IpsetNamePrefix)
	if err != nil {
		common.Log.Errorf("Failed to retrieve list of ipsets")
		return err
	}

	common.Log.Debugf("Got list of ipsets: %v", sets)

	// Must remove references to ipsets by other ipsets before they're destroyed
	for _, s := range sets {
		common.Log.Debugf("Flushing ipset '%s'", string(s))
		if err := ips.Flush(s); err != nil {
			common.Log.Errorf("Failed to flush ipset '%s'", string(s))
			return err
		}
	}

	for _, s := range sets {
		// LocalIPset might be used by WEAVE-NPC-EGRESS chain which we do not
		// flush, so we cannot destroy it.
		if s == npc.LocalIpset {
			continue
		}
		common.Log.Debugf("Destroying ipset '%s'", string(s))
		if err := ips.Destroy(s); err != nil {
			common.Log.Errorf("Failed to destroy ipset '%s'", string(s))
			return err
		}
	}

	return nil
}

func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
	// Configure main chain static rules
	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT"); err != nil {
		return err
	}

	if allowMcast {
		if err := ipt.Append(npc.TableFilter, npc.MainChain,
			"-d", "224.0.0.0/4", "-j", "ACCEPT"); err != nil {
			return err
		}
	}

	// If the destination address is not any of the local pods, let it through
	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "physdev", "--physdev-is-bridged", "--physdev-out="+bridgePortName, "-j", "ACCEPT"); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "NEW", "-j", string(npc.DefaultChain)); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "NEW", "-j", string(npc.IngressChain)); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.EgressMarkChain,
		"-j", "MARK", "--set-xmark", npc.EgressMark); err != nil {
		return err
	}

	// Egress rules:
	//
	// -A WEAVE-NPC-EGRESS -m state --state RELATED,ESTABLISHED -j ACCEPT
	// -A WEAVE-NPC-EGRESS -m physdev --physdev-in=vethwe-bridge -j RETURN
	// -A WEAVE-NPC-EGRESS -d 224.0.0.0/4 -j RETURN
	// -A WEAVE-NPC-EGRESS -m state --state NEW -j WEAVE-NPC-EGRESS-DEFAULT
	// -A WEAVE-NPC-EGRESS -m state --state NEW -m mark ! --mark 0x40000/0x40000 -j WEAVE-NPC-EGRESS-CUSTOM
	// -A WEAVE-NPC-EGRESS -m state --state NEW -m mark ! --mark 0x40000/0x40000 -j NFLOG --nflog-group 86
	// -A WEAVE-NPC-EGRESS -m mark ! --mark 0x40000/0x40000 -j DROP
	//
	// -A WEAVE-NPC-EGRESS-CUSTOM <rulespec> -j MARK --set-xmark 0x40000/0x40000
	// -A WEAVE-NPC-EGRESS-CUSTOM <rulespec> -j RETURN
	//
	// -A WEAVE-NPC-EGRESS-DEFAULT <rulespec> -j MARK --set-xmark 0x40000/0x40000
	// -A WEAVE-NPC-EGRESS-DEFAULT <rulespec> -j RETURN
	//
	// For each rule we create two (mark and return). We cannot just accept
	// a packet if it matches any rule, as a packet might need to traverse
	// the ingress npc as well which happens later in the chain (in some cases
	// we cannot detect whether packet is ingress or egress, so we need to
	// check both chains).

	ruleSpecs := [][]string{
		{"-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT"},
		{"-m", "physdev", "--physdev-is-bridged", "--physdev-in=" + bridgePortName, "-j", "RETURN"},
	}
	if allowMcast {
		ruleSpecs = append(ruleSpecs, []string{"-d", "224.0.0.0/4", "-j", "RETURN"})
	}
	ruleSpecs = append(ruleSpecs, [][]string{
		{"-m", "state", "--state", "NEW", "-j", string(npc.EgressDefaultChain)},
		{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", string(npc.EgressCustomChain)},
		{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", "NFLOG", "--nflog-group", "86"},
		{"-m", "mark", "!", "--mark", npc.EgressMark, "-j", "DROP"},
	}...)
	if err := net.AddChainWithRules(ipt, npc.TableFilter, npc.EgressChain, ruleSpecs); err != nil {
		return err
	}

	// delete `weave-local-pods` ipset which is no longer used by weave-npc
	weaveLocalPodExist, err := ipsetExist(ips, npc.LocalIpset)
	if err != nil {
		common.Log.Errorf("Failed to look if ipset '%s' exists", npc.LocalIpset)
	} else if weaveLocalPodExist {
		common.Log.Debugf("Destroying ipset '%s'", npc.LocalIpset)
		if err := ips.Destroy(npc.LocalIpset); err != nil {
			common.Log.Errorf("Failed to destroy ipset '%s'", npc.LocalIpset)
		}
	}

	return nil
}

// Dummy way to check whether a given ipset exists.
// TODO(brb) Use "ipset -exist create <..>" for our purpose instead (for some reasons
// creating an ipset with -exist fails).
func ipsetExist(ips ipset.Interface, name ipset.Name) (bool, error) {
	sets, err := ips.List(string(name))
	if err != nil {
		return false, err
	}
	for _, s := range sets {
		if s == name {
			return true, nil
		}
	}
	return false, nil
}

func root(cmd *cobra.Command, args []string) {
	var npController cache.Controller

	common.SetLogLevel(logLevel)
	if nodeName == "" {
		// HOSTNAME is set by Kubernetes for pods in the host network namespace
		nodeName = os.Getenv("HOSTNAME")
	}
	if nodeName == "" {
		common.Log.Fatalf("Must set node name via --node-name or $HOSTNAME")
	}
	common.Log.Infof("Starting Weaveworks NPC %s; node name %q", version, nodeName)

	if err := metrics.Start(metricsAddr); err != nil {
		common.Log.Fatalf("Failed to start metrics: %v", err)
	}

	if err := ulogd.Start(); err != nil {
		common.Log.Fatalf("Failed to start ulogd: %v", err)
	}

	config, err := rest.InClusterConfig()
	handleError(err)

	client, err := kubernetes.NewForConfig(config)
	handleError(err)

	ipt, err := iptables.New()
	handleError(err)

	ips := ipset.New(common.LogLogger(), maxList)

	handleError(resetIPTables(ipt))
	handleError(resetIPSets(ips))
	handleError(createBaseRules(ipt, ips))

	npc := npc.New(nodeName, ipt, ips)

	nsController := makeController(client.Core().RESTClient(), "namespaces", &coreapi.Namespace{},
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {
				handleError(npc.AddNamespace(obj.(*coreapi.Namespace)))
			},
			DeleteFunc: func(obj interface{}) {
				switch obj := obj.(type) {
				case *coreapi.Namespace:
					handleError(npc.DeleteNamespace(obj))
				case cache.DeletedFinalStateUnknown:
					// We know this object has gone away, but its final state is no longer
					// available from the API server. Instead we use the last copy of it
					// that we have, which is good enough for our cleanup.
					handleError(npc.DeleteNamespace(obj.Obj.(*coreapi.Namespace)))
				}
			},
			UpdateFunc: func(old, new interface{}) {
				handleError(npc.UpdateNamespace(old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
			}})

	podController := makeController(client.Core().RESTClient(), "pods", &coreapi.Pod{},
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {
				handleError(npc.AddPod(obj.(*coreapi.Pod)))
			},
			DeleteFunc: func(obj interface{}) {
				switch obj := obj.(type) {
				case *coreapi.Pod:
					handleError(npc.DeletePod(obj))
				case cache.DeletedFinalStateUnknown:
					// We know this object has gone away, but its final state is no longer
					// available from the API server. Instead we use the last copy of it
					// that we have, which is good enough for our cleanup.
					handleError(npc.DeletePod(obj.Obj.(*coreapi.Pod)))
				}
			},
			UpdateFunc: func(old, new interface{}) {
				handleError(npc.UpdatePod(old.(*coreapi.Pod), new.(*coreapi.Pod)))
			}})

	npHandlers := cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			handleError(npc.AddNetworkPolicy(obj))
		},
		DeleteFunc: func(obj interface{}) {
			switch obj := obj.(type) {
			case cache.DeletedFinalStateUnknown:
				// We know this object has gone away, but its final state is no longer
				// available from the API server. Instead we use the last copy of it
				// that we have, which is good enough for our cleanup.
				handleError(npc.DeleteNetworkPolicy(obj.Obj))
			default:
				handleError(npc.DeleteNetworkPolicy(obj))
			}
		},
		UpdateFunc: func(old, new interface{}) {
			handleError(npc.UpdateNetworkPolicy(old, new))
		},
	}
	npController = makeController(client.NetworkingV1().RESTClient(), "networkpolicies", &networkingv1.NetworkPolicy{}, npHandlers)

	go nsController.Run(wait.NeverStop)
	go podController.Run(wait.NeverStop)
	go npController.Run(wait.NeverStop)

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	common.Log.Fatalf("Exiting: %v", <-signals)
}

func main() {
	rootCmd := &cobra.Command{
		Use:   "weave-npc",
		Short: "Weaveworks Kubernetes Network Policy Controller",
		Run:   root}

	rootCmd.PersistentFlags().StringVar(&metricsAddr, "metrics-addr", ":6781", "metrics server bind address")
	rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "debug", "logging level (debug, info, warning, error)")
	rootCmd.PersistentFlags().BoolVar(&allowMcast, "allow-mcast", true, "allow all multicast traffic")
	rootCmd.PersistentFlags().StringVar(&nodeName, "node-name", "", "only generate rules that apply to this node")
	rootCmd.PersistentFlags().IntVar(&maxList, "max-list-size", 1024, "maximum size of ipset list (for namespaces)")
	rootCmd.PersistentFlags().StringVar(&bridgePortName, "bridge-port-name", "vethwe-bridge", "name of the brige port on which packets are received and sent")

	handleError(rootCmd.Execute())
}
<commit_msg>skip egress network policies for the traffic node->bridge ip<commit_after>package main

import (
	"os"
	"os/signal"
	"syscall"

	"github.com/coreos/go-iptables/iptables"
	"github.com/spf13/cobra"
	coreapi "k8s.io/api/core/v1"
	networkingv1 "k8s.io/api/networking/v1"
	"k8s.io/apimachinery/pkg/fields"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/cache"

	"github.com/weaveworks/weave/common"
	"github.com/weaveworks/weave/net"
	"github.com/weaveworks/weave/net/ipset"
	"github.com/weaveworks/weave/npc"
	"github.com/weaveworks/weave/npc/metrics"
	"github.com/weaveworks/weave/npc/ulogd"
)

var (
	version        = "unreleased"
	metricsAddr    string
	logLevel       string
	allowMcast     bool
	nodeName       string
	maxList        int
	bridgePortName string
)

func handleError(err error) { common.CheckFatal(err) }

func makeController(getter cache.Getter, resource string,
	objType runtime.Object, handlers cache.ResourceEventHandlerFuncs) cache.Controller {
	listWatch := cache.NewListWatchFromClient(getter, resource, "", fields.Everything())
	_, controller := cache.NewInformer(listWatch, objType, 0, handlers)
	return controller
}

func resetIPTables(ipt *iptables.IPTables) error {
	// Flush chains first so there are no refs to extant ipsets
	if err := ipt.ClearChain(npc.TableFilter, npc.IngressChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.DefaultChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.MainChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressMarkChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressCustomChain); err != nil {
		return err
	}

	if err := ipt.ClearChain(npc.TableFilter, npc.EgressDefaultChain); err != nil {
		return err
	}

	// We do not clear npc.EgressChain here because otherwise, in the case of restarting
	// weave-npc process, all egress traffic is allowed for a short period of time.
	// The chain is created in createBaseRules.

	return nil
}

func resetIPSets(ips ipset.Interface) error {
	// Remove ipsets prefixed `weave-` only.

	sets, err := ips.List(npc.IpsetNamePrefix)
	if err != nil {
		common.Log.Errorf("Failed to retrieve list of ipsets")
		return err
	}

	common.Log.Debugf("Got list of ipsets: %v", sets)

	// Must remove references to ipsets by other ipsets before they're destroyed
	for _, s := range sets {
		common.Log.Debugf("Flushing ipset '%s'", string(s))
		if err := ips.Flush(s); err != nil {
			common.Log.Errorf("Failed to flush ipset '%s'", string(s))
			return err
		}
	}

	for _, s := range sets {
		// LocalIPset might be used by WEAVE-NPC-EGRESS chain which we do not
		// flush, so we cannot destroy it.
		if s == npc.LocalIpset {
			continue
		}
		common.Log.Debugf("Destroying ipset '%s'", string(s))
		if err := ips.Destroy(s); err != nil {
			common.Log.Errorf("Failed to destroy ipset '%s'", string(s))
			return err
		}
	}

	return nil
}

func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
	// Configure main chain static rules
	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT"); err != nil {
		return err
	}

	if allowMcast {
		if err := ipt.Append(npc.TableFilter, npc.MainChain,
			"-d", "224.0.0.0/4", "-j", "ACCEPT"); err != nil {
			return err
		}
	}

	// If the destination address is not any of the local pods, let it through
	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "physdev", "--physdev-is-bridged", "--physdev-out="+bridgePortName, "-j", "ACCEPT"); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "NEW", "-j", string(npc.DefaultChain)); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.MainChain,
		"-m", "state", "--state", "NEW", "-j", string(npc.IngressChain)); err != nil {
		return err
	}

	if err := ipt.Append(npc.TableFilter, npc.EgressMarkChain,
		"-j", "MARK", "--set-xmark", npc.EgressMark); err != nil {
		return err
	}

	// Egress rules:
	//
	// -A WEAVE-NPC-EGRESS -m state --state RELATED,ESTABLISHED -j ACCEPT
	// -A WEAVE-NPC-EGRESS -m physdev --physdev-in vethwe-bridge --physdev-is-bridged -j RETURN
	// -A WEAVE-NPC-EGRESS -m addrtype --dst-type LOCAL -j RETURN
	// -A WEAVE-NPC-EGRESS -m state --state NEW -j WEAVE-NPC-EGRESS-DEFAULT
	// -A WEAVE-NPC-EGRESS -m state --state NEW -m mark ! --mark 0x40000/0x40000 -j WEAVE-NPC-EGRESS-CUSTOM
	// -A WEAVE-NPC-EGRESS -m state --state NEW -m mark ! --mark 0x40000/0x40000 -j NFLOG --nflog-group 86
	// -A WEAVE-NPC-EGRESS -m mark ! --mark 0x40000/0x40000 -j DROP
	//
	// -A WEAVE-NPC-EGRESS-CUSTOM <rulespec> -j MARK --set-xmark 0x40000/0x40000
	// -A WEAVE-NPC-EGRESS-CUSTOM <rulespec> -j RETURN
	//
	// -A WEAVE-NPC-EGRESS-DEFAULT <rulespec> -j MARK --set-xmark 0x40000/0x40000
	// -A WEAVE-NPC-EGRESS-DEFAULT <rulespec> -j RETURN
	//
	// For each rule we create two (mark and return). We cannot just accept
	// a packet if it matches any rule, as a packet might need to traverse
	// the ingress npc as well which happens later in the chain (in some cases
	// we cannot detect whether packet is ingress or egress, so we need to
	// check both chains).

	ruleSpecs := [][]string{
		{"-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT"},
		// skip running through egress network policies for the traffic not coming from local pods
		{"-m", "physdev", "--physdev-is-bridged", "--physdev-in=" + bridgePortName, "-j", "RETURN"},
		// skip running through egress network policies for the traffic bound for IP address assigned for the bridge
		{"-m", "addrtype", "--dst-type", "LOCAL", "-j", "RETURN"},
	}
	if allowMcast {
		ruleSpecs = append(ruleSpecs, []string{"-d", "224.0.0.0/4", "-j", "RETURN"})
	}
	ruleSpecs = append(ruleSpecs, [][]string{
		{"-m", "state", "--state", "NEW", "-j", string(npc.EgressDefaultChain)},
		{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", string(npc.EgressCustomChain)},
		{"-m", "state", "--state", "NEW", "-m", "mark", "!", "--mark", npc.EgressMark, "-j", "NFLOG", "--nflog-group", "86"},
		{"-m", "mark", "!", "--mark", npc.EgressMark, "-j", "DROP"},
	}...)
	if err := net.AddChainWithRules(ipt, npc.TableFilter, npc.EgressChain, ruleSpecs); err != nil {
		return err
	}

	// delete `weave-local-pods` ipset which is no longer used by weave-npc
	weaveLocalPodExist, err := ipsetExist(ips, npc.LocalIpset)
	if err != nil {
		common.Log.Errorf("Failed to look if ipset '%s' exists", npc.LocalIpset)
	} else if weaveLocalPodExist {
		common.Log.Debugf("Destroying ipset '%s'", npc.LocalIpset)
		if err := ips.Destroy(npc.LocalIpset); err != nil {
			common.Log.Errorf("Failed to destroy ipset '%s'", npc.LocalIpset)
		}
	}

	return nil
}

// Dummy way to check whether a given ipset exists.
// TODO(brb) Use "ipset -exist create <..>" for our purpose instead (for some reasons
// creating an ipset with -exist fails).
func ipsetExist(ips ipset.Interface, name ipset.Name) (bool, error) {
	sets, err := ips.List(string(name))
	if err != nil {
		return false, err
	}
	for _, s := range sets {
		if s == name {
			return true, nil
		}
	}
	return false, nil
}

func root(cmd *cobra.Command, args []string) {
	var npController cache.Controller

	common.SetLogLevel(logLevel)
	if nodeName == "" {
		// HOSTNAME is set by Kubernetes for pods in the host network namespace
		nodeName = os.Getenv("HOSTNAME")
	}
	if nodeName == "" {
		common.Log.Fatalf("Must set node name via --node-name or $HOSTNAME")
	}
	common.Log.Infof("Starting Weaveworks NPC %s; node name %q", version, nodeName)

	if err := metrics.Start(metricsAddr); err != nil {
		common.Log.Fatalf("Failed to start metrics: %v", err)
	}

	if err := ulogd.Start(); err != nil {
		common.Log.Fatalf("Failed to start ulogd: %v", err)
	}

	config, err := rest.InClusterConfig()
	handleError(err)

	client, err := kubernetes.NewForConfig(config)
	handleError(err)

	ipt, err := iptables.New()
	handleError(err)

	ips := ipset.New(common.LogLogger(), maxList)

	handleError(resetIPTables(ipt))
	handleError(resetIPSets(ips))
	handleError(createBaseRules(ipt, ips))

	npc := npc.New(nodeName, ipt, ips)

	nsController := makeController(client.Core().RESTClient(), "namespaces", &coreapi.Namespace{},
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {
				handleError(npc.AddNamespace(obj.(*coreapi.Namespace)))
			},
			DeleteFunc: func(obj interface{}) {
				switch obj := obj.(type) {
				case *coreapi.Namespace:
					handleError(npc.DeleteNamespace(obj))
				case cache.DeletedFinalStateUnknown:
					// We know this object has gone away, but its final state is no longer
					// available from the API server. Instead we use the last copy of it
					// that we have, which is good enough for our cleanup.
					handleError(npc.DeleteNamespace(obj.Obj.(*coreapi.Namespace)))
				}
			},
			UpdateFunc: func(old, new interface{}) {
				handleError(npc.UpdateNamespace(old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
			}})

	podController := makeController(client.Core().RESTClient(), "pods", &coreapi.Pod{},
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {
				handleError(npc.AddPod(obj.(*coreapi.Pod)))
			},
			DeleteFunc: func(obj interface{}) {
				switch obj := obj.(type) {
				case *coreapi.Pod:
					handleError(npc.DeletePod(obj))
				case cache.DeletedFinalStateUnknown:
					// We know this object has gone away, but its final state is no longer
					// available from the API server. Instead we use the last copy of it
					// that we have, which is good enough for our cleanup.
					handleError(npc.DeletePod(obj.Obj.(*coreapi.Pod)))
				}
			},
			UpdateFunc: func(old, new interface{}) {
				handleError(npc.UpdatePod(old.(*coreapi.Pod), new.(*coreapi.Pod)))
			}})

	npHandlers := cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			handleError(npc.AddNetworkPolicy(obj))
		},
		DeleteFunc: func(obj interface{}) {
			switch obj := obj.(type) {
			case cache.DeletedFinalStateUnknown:
				// We know this object has gone away, but its final state is no longer
				// available from the API server. Instead we use the last copy of it
				// that we have, which is good enough for our cleanup.
				handleError(npc.DeleteNetworkPolicy(obj.Obj))
			default:
				handleError(npc.DeleteNetworkPolicy(obj))
			}
		},
		UpdateFunc: func(old, new interface{}) {
			handleError(npc.UpdateNetworkPolicy(old, new))
		},
	}
	npController = makeController(client.NetworkingV1().RESTClient(), "networkpolicies", &networkingv1.NetworkPolicy{}, npHandlers)

	go nsController.Run(wait.NeverStop)
	go podController.Run(wait.NeverStop)
	go npController.Run(wait.NeverStop)

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
	common.Log.Fatalf("Exiting: %v", <-signals)
}

func main() {
	rootCmd := &cobra.Command{
		Use:   "weave-npc",
		Short: "Weaveworks Kubernetes Network Policy Controller",
		Run:   root}

	rootCmd.PersistentFlags().StringVar(&metricsAddr, "metrics-addr", ":6781", "metrics server bind address")
	rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "debug", "logging level (debug, info, warning, error)")
	rootCmd.PersistentFlags().BoolVar(&allowMcast, "allow-mcast", true, "allow all multicast traffic")
	rootCmd.PersistentFlags().StringVar(&nodeName, "node-name", "", "only generate rules that apply to this node")
	rootCmd.PersistentFlags().IntVar(&maxList, "max-list-size", 1024, "maximum size of ipset list (for namespaces)")
	rootCmd.PersistentFlags().StringVar(&bridgePortName, "bridge-port-name", "vethwe-bridge", "name of the brige port on which packets are received and sent")

	handleError(rootCmd.Execute())
}
<|endoftext|>


<commit_before>package main

import (
	"net/http"
	"os"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/weaveworks/weave/ipam"
	"github.com/weaveworks/weave/nameserver"
	"github.com/weaveworks/weave/net/address"
	weave "github.com/weaveworks/weave/router"
)

func metricsHandler(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) http.Handler {
	reg := prometheus.NewRegistry()
	reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
	reg.MustRegister(newMetrics(router, allocator, ns, dnsserver))
	return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
}

type collector struct {
	router    *weave.NetworkRouter
	allocator *ipam.Allocator
	ns        *nameserver.Nameserver
	dnsserver *nameserver.DNSServer
}

type metric struct {
	*prometheus.Desc
	Collect func(WeaveStatus, *prometheus.Desc, chan<- prometheus.Metric)
}

func desc(fqName, help string, variableLabels ...string) *prometheus.Desc {
	return prometheus.NewDesc(fqName, help, variableLabels, prometheus.Labels{})
}

func intGauge(desc *prometheus.Desc, val int, labels ...string) prometheus.Metric {
	return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(val), labels...)
}
func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) prometheus.Metric {
	return prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(val), labels...)
}

var metrics []metric = []metric{
	{desc("weave_connections", "Number of peer-to-peer connections.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			established := 0
			for _, conn := range s.Router.Connections {
				if conn.State == "established" {
					established++
				}
			}

			ch <- intGauge(desc, len(s.Router.Connections)-established, "non-established")
			ch <- intGauge(desc, established, "established")
		}},
	{desc("weave_connection_termination_count", "Number of peer-to-peer connections terminated."),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			ch <- uint64Counter(desc, uint64(s.Router.TerminationCount))
		}},
	{desc("weave_ips", "Number of IP addresses.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if s.IPAM != nil {
				ch <- intGauge(desc, s.IPAM.RangeNumIPs, "total")
				ch <- intGauge(desc, s.IPAM.ActiveIPs, "local-used")
			}
		}},
	{desc("weave_dns_entries", "Number of DNS entries.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if s.DNS != nil {
				ch <- intGauge(desc, countDNSEntries(s.DNS.Entries), "total")
				ch <- intGauge(desc, countDNSEntriesForPeer(s.Router.Name, s.DNS.Entries), "local")
			}
		}},
	{desc("weave_flows", "Number of FastDP flows."),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if metrics := fastDPMetrics(s); metrics != nil {
				ch <- intGauge(desc, metrics.Flows)
			}
		}},
}

func fastDPMetrics(s WeaveStatus) *weave.FastDPMetrics {
	if diagMap, ok := s.Router.OverlayDiagnostics.(map[string]interface{}); ok {
		if diag, ok := diagMap["fastdp"]; ok {
			if fastDPStats, ok := diag.(weave.FastDPStatus); ok {
				return fastDPStats.Metrics().(*weave.FastDPMetrics)
			}
		}
	}
	return nil
}

func newMetrics(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) *collector {
	return &collector{
		router:    router,
		allocator: allocator,
		ns:        ns,
		dnsserver: dnsserver,
	}
}

func (m *collector) Collect(ch chan<- prometheus.Metric) {

	status := WeaveStatus{"", nil,
		weave.NewNetworkRouterStatus(m.router),
		ipam.NewStatus(m.allocator, address.CIDR{}),
		nameserver.NewStatus(m.ns, m.dnsserver)}

	for _, metric := range metrics {
		metric.Collect(status, metric.Desc, ch)
	}
}

func (m *collector) Describe(ch chan<- *prometheus.Desc) {
	for _, metric := range metrics {
		ch <- metric.Desc
	}
}
<commit_msg>Rename weave_connection_termination_count metric<commit_after>package main

import (
	"net/http"
	"os"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"

	"github.com/weaveworks/weave/ipam"
	"github.com/weaveworks/weave/nameserver"
	"github.com/weaveworks/weave/net/address"
	weave "github.com/weaveworks/weave/router"
)

func metricsHandler(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) http.Handler {
	reg := prometheus.NewRegistry()
	reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
	reg.MustRegister(newMetrics(router, allocator, ns, dnsserver))
	return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
}

type collector struct {
	router    *weave.NetworkRouter
	allocator *ipam.Allocator
	ns        *nameserver.Nameserver
	dnsserver *nameserver.DNSServer
}

type metric struct {
	*prometheus.Desc
	Collect func(WeaveStatus, *prometheus.Desc, chan<- prometheus.Metric)
}

func desc(fqName, help string, variableLabels ...string) *prometheus.Desc {
	return prometheus.NewDesc(fqName, help, variableLabels, prometheus.Labels{})
}

func intGauge(desc *prometheus.Desc, val int, labels ...string) prometheus.Metric {
	return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, float64(val), labels...)
}
func uint64Counter(desc *prometheus.Desc, val uint64, labels ...string) prometheus.Metric {
	return prometheus.MustNewConstMetric(desc, prometheus.CounterValue, float64(val), labels...)
}

var metrics []metric = []metric{
	{desc("weave_connections", "Number of peer-to-peer connections.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			established := 0
			for _, conn := range s.Router.Connections {
				if conn.State == "established" {
					established++
				}
			}

			ch <- intGauge(desc, len(s.Router.Connections)-established, "non-established")
			ch <- intGauge(desc, established, "established")
		}},
	{desc("weave_connection_terminations_total", "Number of peer-to-peer connections terminated."),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			ch <- uint64Counter(desc, uint64(s.Router.TerminationCount))
		}},
	{desc("weave_ips", "Number of IP addresses.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if s.IPAM != nil {
				ch <- intGauge(desc, s.IPAM.RangeNumIPs, "total")
				ch <- intGauge(desc, s.IPAM.ActiveIPs, "local-used")
			}
		}},
	{desc("weave_dns_entries", "Number of DNS entries.", "state"),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if s.DNS != nil {
				ch <- intGauge(desc, countDNSEntries(s.DNS.Entries), "total")
				ch <- intGauge(desc, countDNSEntriesForPeer(s.Router.Name, s.DNS.Entries), "local")
			}
		}},
	{desc("weave_flows", "Number of FastDP flows."),
		func(s WeaveStatus, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
			if metrics := fastDPMetrics(s); metrics != nil {
				ch <- intGauge(desc, metrics.Flows)
			}
		}},
}

func fastDPMetrics(s WeaveStatus) *weave.FastDPMetrics {
	if diagMap, ok := s.Router.OverlayDiagnostics.(map[string]interface{}); ok {
		if diag, ok := diagMap["fastdp"]; ok {
			if fastDPStats, ok := diag.(weave.FastDPStatus); ok {
				return fastDPStats.Metrics().(*weave.FastDPMetrics)
			}
		}
	}
	return nil
}

func newMetrics(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) *collector {
	return &collector{
		router:    router,
		allocator: allocator,
		ns:        ns,
		dnsserver: dnsserver,
	}
}

func (m *collector) Collect(ch chan<- prometheus.Metric) {

	status := WeaveStatus{"", nil,
		weave.NewNetworkRouterStatus(m.router),
		ipam.NewStatus(m.allocator, address.CIDR{}),
		nameserver.NewStatus(m.ns, m.dnsserver)}

	for _, metric := range metrics {
		metric.Collect(status, metric.Desc, ch)
	}
}

func (m *collector) Describe(ch chan<- *prometheus.Desc) {
	for _, metric := range metrics {
		ch <- metric.Desc
	}
}
<|endoftext|>


<commit_before>package concurrent

//----------------------------------------------------------------------
// This file is part of Gospel.
// Copyright (C) 2011-2020 Bernd Fix
//
// Gospel is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Gospel is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------

import (
	"errors"
	"time"
)

// Error codes
var (
	ErrSigInactive   = errors.New("signaller inactive")
	ErrSigNoListener = errors.New("no signal listener")
)

//----------------------------------------------------------------------

// Signal can be any object (intrinsic or custom); it is the responsibility of
// the senders and receivers of signals to handle them accordingly.
type Signal interface{}

//----------------------------------------------------------------------

// Listener for signals managed by Signaller
type Listener struct {
	ch   chan Signal // channel to receive on
	refs int         // number of pending dispatches
}

// Signal returns the channel from which to read the signal.
func (l *Listener) Signal() <-chan Signal {
	return l.ch
}

//----------------------------------------------------------------------
// Signaller (signal dispatcher to listeners)
//----------------------------------------------------------------------

// Signaller dispatches signals to multiple concurrent listeners. The sequence
// in which listeners are served is stochastic.
//
// In highly concurrent environments with a lot of messages the sequence of
// signals seen by a listener can vary. This is due to the fact that a signal
// gets dispatched in a Go routine, so the next signal can be dispatched
// before a listener got the first one if the second Go routine handles the
// listener earlier. It is therefore mandatory that received signals from
// a listener get handled in a Go routine as well to keep latency low. If
// a listener violates that promise, it got removed from the list.
type Signaller struct {
	inCh   chan Signal        // channel for incoming signals
	outChs map[*Listener]bool // channels for out-going signals

	cmdCh      chan *listenerOp // internal channel to synchronize maintenance
	resCh      chan interface{} // channel for command results
	active     bool             // is the signaller dispatching signals?
	maxLatency time.Duration    // max time for listener to respond
}

// NewSignaller instantiates a new signal manager:
func NewSignaller() *Signaller {
	// create a new instance and initialize it.
	s := &Signaller{
		inCh:       make(chan Signal),
		outChs:     make(map[*Listener]bool),
		cmdCh:      make(chan *listenerOp),
		resCh:      make(chan interface{}),
		active:     true,
		maxLatency: time.Second,
	}
	// run the dispatch loop as long as the signaller is active.
	go func() {
		for s.active {
			select {
			// handle listener list operation
			case cmd := <-s.cmdCh:
				switch cmd.op {
				// create a new listener channel
				case sigListenerAdd:
					listener := &Listener{
						ch:   make(chan Signal),
						refs: 0,
					}
					s.outChs[listener] = true
					s.resCh <- listener

				// remove listener from list
				case sigListenerDrop:
					var err error
					if _, ok := s.outChs[cmd.lst]; !ok {
						err = ErrSigNoListener
					} else {
						// remove from list
						delete(s.outChs, cmd.lst)
						// close unreferenced channels
						if cmd.lst.refs == 0 {
							close(cmd.lst.ch)
						}
					}
					s.resCh <- err
				}

			// dispatch received signals
			case sig := <-s.inCh:
				// create a list of currently active listeners
				// so we can serve them in a Go routine.
				active := make([]*Listener, 0)
				for lst := range s.outChs {
					active = append(active, lst)
					// increment pending count on listener
					lst.refs++
				}
				go func() {
					for _, listener := range active {
						done := make(chan interface{})
						go func() {
							defer func() {
								// decrease pending count on listener
								listener.refs--
							}()
							listener.ch <- sig
							close(done)
						}()
						select {
						case <-time.After(time.Second):
							// listener not responding: drop it
							s.Drop(listener)

						// message sent
						case <-done:
						}
					}
				}()
			}
		}
	}()
	return s
}

// SetLatency sets the max latency for listener. A listener is removed from
// the list if it violates this policy.
func (s *Signaller) SetLatency(d time.Duration) {
	s.maxLatency = d
}

// Retire a signaller: This will terminate the dispatch loop for signals; no
// further send or listen operations are supported. A retired signaller cannot
// be re-activated.
func (s *Signaller) Retire() {
	s.active = false
}

//----------------------------------------------------------------------

// Send a signal to be dispatched to all listeners.
func (s *Signaller) Send(sig Signal) error {
	// check for active signaller
	if !s.active {
		return ErrSigInactive
	}
	s.inCh <- sig
	return nil
}

//----------------------------------------------------------------------

// Listener returns a new channel to listen on each time it is called.
// Function interested in listening should get the channel, start the
// for/select loop and drop the channel if the loop terminates.
// Requesting an listener and than not reading from it will block all
// other listeners of the signaller.
func (s *Signaller) Listener() (*Listener, error) {
	// check for active signaller
	if !s.active {
		return nil, ErrSigInactive
	}
	// trigger add operation.
	s.cmdCh <- &listenerOp{op: sigListenerAdd}
	return (<-s.resCh).(*Listener), nil
}

// Drop removes a listener from the list. Failing to drop or close a
// listener will result in hanging go routines.
func (s *Signaller) Drop(listener *Listener) error {
	// check for active signaller
	if !s.active {
		return ErrSigInactive
	}
	// trigger delete operation
	s.cmdCh <- &listenerOp{
		lst: listener,
		op:  sigListenerDrop,
	}
	// handle error return for command.
	var err error
	res := <-s.resCh
	if res != nil {
		err = res.(error)
	}
	return err
}

//----------------------------------------------------------------------

// ListenerOp codes
const (
	sigListenerAdd = iota
	sigListenerDrop
	sigListenerRef
	sigListenerUnref
)

// listenerOp represents an operation on the listener list:
type listenerOp struct {
	op  int       // sigListener????
	lst *Listener // listener reference
}
<commit_msg>Use custom max. latency for listener reads.<commit_after>package concurrent

//----------------------------------------------------------------------
// This file is part of Gospel.
// Copyright (C) 2011-2020 Bernd Fix
//
// Gospel is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Gospel is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later
//----------------------------------------------------------------------

import (
	"errors"
	"time"
)

// Error codes
var (
	ErrSigInactive   = errors.New("signaller inactive")
	ErrSigNoListener = errors.New("no signal listener")
)

//----------------------------------------------------------------------

// Signal can be any object (intrinsic or custom); it is the responsibility of
// the senders and receivers of signals to handle them accordingly.
type Signal interface{}

//----------------------------------------------------------------------

// Listener for signals managed by Signaller
type Listener struct {
	ch   chan Signal // channel to receive on
	refs int         // number of pending dispatches
}

// Signal returns the channel from which to read the signal.
func (l *Listener) Signal() <-chan Signal {
	return l.ch
}

//----------------------------------------------------------------------
// Signaller (signal dispatcher to listeners)
//----------------------------------------------------------------------

// Signaller dispatches signals to multiple concurrent listeners. The sequence
// in which listeners are served is stochastic.
//
// In highly concurrent environments with a lot of messages the sequence of
// signals seen by a listener can vary. This is due to the fact that a signal
// gets dispatched in a Go routine, so the next signal can be dispatched
// before a listener got the first one if the second Go routine handles the
// listener earlier. It is therefore mandatory that received signals from
// a listener get handled in a Go routine as well to keep latency low. If
// a listener violates that promise, it got removed from the list.
type Signaller struct {
	inCh   chan Signal        // channel for incoming signals
	outChs map[*Listener]bool // channels for out-going signals

	cmdCh      chan *listenerOp // internal channel to synchronize maintenance
	resCh      chan interface{} // channel for command results
	active     bool             // is the signaller dispatching signals?
	maxLatency time.Duration    // max time for listener to respond
}

// NewSignaller instantiates a new signal manager:
func NewSignaller() *Signaller {
	// create a new instance and initialize it.
	s := &Signaller{
		inCh:       make(chan Signal),
		outChs:     make(map[*Listener]bool),
		cmdCh:      make(chan *listenerOp),
		resCh:      make(chan interface{}),
		active:     true,
		maxLatency: time.Second,
	}
	// run the dispatch loop as long as the signaller is active.
	go func() {
		for s.active {
			select {
			// handle listener list operation
			case cmd := <-s.cmdCh:
				switch cmd.op {
				// create a new listener channel
				case sigListenerAdd:
					listener := &Listener{
						ch:   make(chan Signal),
						refs: 0,
					}
					s.outChs[listener] = true
					s.resCh <- listener

				// remove listener from list
				case sigListenerDrop:
					var err error
					if _, ok := s.outChs[cmd.lst]; !ok {
						err = ErrSigNoListener
					} else {
						// remove from list
						delete(s.outChs, cmd.lst)
						// close unreferenced channels
						if cmd.lst.refs == 0 {
							close(cmd.lst.ch)
						}
					}
					s.resCh <- err
				}

			// dispatch received signals
			case sig := <-s.inCh:
				// create a list of currently active listeners
				// so we can serve them in a Go routine.
				active := make([]*Listener, 0)
				for lst := range s.outChs {
					active = append(active, lst)
					// increment pending count on listener
					lst.refs++
				}
				go func() {
					for _, listener := range active {
						done := make(chan struct{})
						go func() {
							defer func() {
								// decrease pending count on listener
								listener.refs--
							}()
							listener.ch <- sig
							close(done)
						}()
						select {
						case <-time.After(s.maxLatency):
							// listener not responding: drop it
							s.Drop(listener)

						// message sent
						case <-done:
						}
					}
				}()
			}
		}
	}()
	return s
}

// SetLatency sets the max latency for listener. A listener is removed from
// the list if it violates this policy.
func (s *Signaller) SetLatency(d time.Duration) {
	s.maxLatency = d
}

// Retire a signaller: This will terminate the dispatch loop for signals; no
// further send or listen operations are supported. A retired signaller cannot
// be re-activated.
func (s *Signaller) Retire() {
	s.active = false
}

//----------------------------------------------------------------------

// Send a signal to be dispatched to all listeners.
func (s *Signaller) Send(sig Signal) error {
	// check for active signaller
	if !s.active {
		return ErrSigInactive
	}
	s.inCh <- sig
	return nil
}

//----------------------------------------------------------------------

// Listener returns a new channel to listen on each time it is called.
// Function interested in listening should get the channel, start the
// for/select loop and drop the channel if the loop terminates.
// Requesting an listener and than not reading from it will block all
// other listeners of the signaller.
func (s *Signaller) Listener() (*Listener, error) {
	// check for active signaller
	if !s.active {
		return nil, ErrSigInactive
	}
	// trigger add operation.
	s.cmdCh <- &listenerOp{op: sigListenerAdd}
	return (<-s.resCh).(*Listener), nil
}

// Drop removes a listener from the list. Failing to drop or close a
// listener will result in hanging go routines.
func (s *Signaller) Drop(listener *Listener) error {
	// check for active signaller
	if !s.active {
		return ErrSigInactive
	}
	// trigger delete operation
	s.cmdCh <- &listenerOp{
		lst: listener,
		op:  sigListenerDrop,
	}
	// handle error return for command.
	var err error
	res := <-s.resCh
	if res != nil {
		err = res.(error)
	}
	return err
}

//----------------------------------------------------------------------

// ListenerOp codes
const (
	sigListenerAdd = iota
	sigListenerDrop
	sigListenerRef
	sigListenerUnref
)

// listenerOp represents an operation on the listener list:
type listenerOp struct {
	op  int       // sigListener????
	lst *Listener // listener reference
}
<|endoftext|>


<commit_before>// Copyright 2014 The roc Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rocserv

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"time"

	"gitlab.pri.ibanyu.com/middleware/seaweed/xfile"
	"gitlab.pri.ibanyu.com/middleware/seaweed/xlog"
	"gitlab.pri.ibanyu.com/middleware/seaweed/xnet/xhttp"

	"github.com/julienschmidt/httprouter"
)

type backDoorHttp struct {
}

var (
	serviceMD5  string
	startUpTime string
)

func (m *backDoorHttp) Init() error {
	if len(os.Args) > 0 {
		filePath, err := os.Executable()
		if err == nil {
			md5, err := xfile.MD5Sum(filePath)
			if err == nil {
				serviceMD5 = fmt.Sprintf("%x", md5)
			}
		}
	}
	startUpTime = time.Now().Format("2006-01-02 15:04:05")
	return nil
}

func (m *backDoorHttp) Driver() (string, interface{}) {
	//fun := "backDoorHttp.Driver -->"

	router := httprouter.New()
	// 重启
	router.POST("/backdoor/restart", xhttp.HttpRequestWrapper(FactoryRestart))

	// healthcheck
	router.GET("/backdoor/health/check", xhttp.HttpRequestWrapper(FactoryHealthCheck))

	// 获取实例md5值
	router.GET("/backdoor/md5", xhttp.HttpRequestWrapper(FactoryMD5))

	return "0.0.0.0:60000", router
}

// ==============================
type Restart struct {
}

func FactoryRestart() xhttp.HandleRequest {
	return new(Restart)
}

func (m *Restart) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	xlog.Infof(context.Background(), "RECEIVE RESTART COMMAND")
	server.sbase.Stop()
	os.Exit(1)
	// 这里的代码执行不到了，因为之前已经退出了
	return xhttp.NewHttpRespString(200, "{}")
}

// ==============================
type HealthCheck struct {
}

func FactoryHealthCheck() xhttp.HandleRequest {
	return new(HealthCheck)
}

func (m *HealthCheck) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	fun := "HealthCheck -->"
	xlog.Infof(context.Background(), "%s in", fun)

	return xhttp.NewHttpRespString(200, "{}")
}

//MD5 ...
type MD5 struct {
}

//FactoryMD5 ...
func FactoryMD5() xhttp.HandleRequest {
	return new(MD5)
}

func (m *MD5) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	res := struct {
		Md5     string `json:"md5"`
		StartUp string `json:"start_up"`
	}{
		Md5:     serviceMD5,
		StartUp: startUpTime,
	}
	s, _ := json.Marshal(res)
	return xhttp.NewHttpRespString(200, string(s))
}
<commit_msg>从环境变量中取出backdoor端口 (#219)<commit_after>// Copyright 2014 The roc Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package rocserv

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"time"

	"gitlab.pri.ibanyu.com/middleware/seaweed/xfile"
	"gitlab.pri.ibanyu.com/middleware/seaweed/xlog"
	"gitlab.pri.ibanyu.com/middleware/seaweed/xnet/xhttp"

	"github.com/julienschmidt/httprouter"
)

type backDoorHttp struct {
}

var (
	serviceMD5  string
	startUpTime string
)

func (m *backDoorHttp) Init() error {
	if len(os.Args) > 0 {
		filePath, err := os.Executable()
		if err == nil {
			md5, err := xfile.MD5Sum(filePath)
			if err == nil {
				serviceMD5 = fmt.Sprintf("%x", md5)
			}
		}
	}
	startUpTime = time.Now().Format("2006-01-02 15:04:05")
	return nil
}

func (m *backDoorHttp) Driver() (string, interface{}) {
	//fun := "backDoorHttp.Driver -->"

	router := httprouter.New()
	backDoorPort := os.Getenv("BACKDOORPORT")
	if backDoorPort == "" {
		backDoorPort = "60000"
	}
	// 重启
	router.POST("/backdoor/restart", xhttp.HttpRequestWrapper(FactoryRestart))

	// healthcheck
	router.GET("/backdoor/health/check", xhttp.HttpRequestWrapper(FactoryHealthCheck))

	// 获取实例md5值
	router.GET("/backdoor/md5", xhttp.HttpRequestWrapper(FactoryMD5))

	return fmt.Sprintf("0.0.0.0:%s", backDoorPort), router
}

// ==============================
type Restart struct {
}

func FactoryRestart() xhttp.HandleRequest {
	return new(Restart)
}

func (m *Restart) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	xlog.Infof(context.Background(), "RECEIVE RESTART COMMAND")
	server.sbase.Stop()
	os.Exit(1)
	// 这里的代码执行不到了，因为之前已经退出了
	return xhttp.NewHttpRespString(200, "{}")
}

// ==============================
type HealthCheck struct {
}

func FactoryHealthCheck() xhttp.HandleRequest {
	return new(HealthCheck)
}

func (m *HealthCheck) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	fun := "HealthCheck -->"
	xlog.Infof(context.Background(), "%s in", fun)

	return xhttp.NewHttpRespString(200, "{}")
}

//MD5 ...
type MD5 struct {
}

//FactoryMD5 ...
func FactoryMD5() xhttp.HandleRequest {
	return new(MD5)
}

func (m *MD5) Handle(r *xhttp.HttpRequest) xhttp.HttpResponse {
	res := struct {
		Md5     string `json:"md5"`
		StartUp string `json:"start_up"`
	}{
		Md5:     serviceMD5,
		StartUp: startUpTime,
	}
	s, _ := json.Marshal(res)
	return xhttp.NewHttpRespString(200, string(s))
}
<|endoftext|>


<commit_before>package backupservice

import (
	"log"
	"os"

	"fmt"

	"path/filepath"

	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/mleonard87/frosty/config"
)

const (
	BUCKET_NAME                            string = "frosty.backups"
	ERROR_CODE_INVALID_BUCKET_NAME         string = "InvalidBucketName"
	ERROR_CODE_BUCKET_ALREADY_OWNED_BY_YOU string = "BucketAlreadyOwnedByYou"
)

type AmazonS3BackupService struct {
	AccessKeyId     string
	SecretAccessKey string
	Region          string
	AccountId       string
	RetentionDays   int64
	BucketName      string
	S3Service       *s3.S3
}

// Return the backup service type this must match the string as used as the JSON property in the frosty backup config.
func (agss *AmazonS3BackupService) Name() string {
	return config.BACKUP_SERVICE_AMAZON_S3
}

// Initialise any variable needed for backups.
func (asbs *AmazonS3BackupService) SetConfig(backupConfig *config.BackupConfig) {
	asbs.AccessKeyId = backupConfig.BackupConfig["accessKeyId"].(string)
	asbs.SecretAccessKey = backupConfig.BackupConfig["secretAccessKey"].(string)
	asbs.Region = backupConfig.BackupConfig["region"].(string)
	asbs.AccountId = backupConfig.BackupConfig["accountId"].(string)
	asbs.RetentionDays = int64(backupConfig.BackupConfig["retentionDays"].(float64))
	asbs.BucketName = BUCKET_NAME
}

// Initialise anything in the backup service that needs to be created prior to uploading files. In this instance we need
// to create a bucket to store the backups if one does not already exist. This always uses a bucket
// called "frosty.backups".
func (asbs *AmazonS3BackupService) Init() error {
	asbs.setEnvvars()
	asbs.S3Service = s3.New(session.New(), &aws.Config{})

	err := asbs.createBucket(asbs.BucketName)
	if err != nil {
		fmt.Println("Error creating bucket")
		fmt.Println(err)
		return err
	}

	err = asbs.putBucketLifecycleConfiguration()
	if err != nil {
		fmt.Println("Error creating bucket lifecycle")
		fmt.Println(err)
		return err
	}

	return nil
}

// Store the file in pathToFile in the bucket in S3.
func (asbs *AmazonS3BackupService) StoreFile(pathToFile string) error {
	_, fileName := filepath.Split(pathToFile)

	key := getObjectKey(fileName)

	f, err := os.Open(pathToFile)
	if err != nil {
		log.Printf("Failed to open file to store: %s", pathToFile)
		log.Println(err)
		return err
	}

	params := &s3.PutObjectInput{
		Body:   f,
		Bucket: &asbs.BucketName,
		Key:    &key,
	}

	_, err = asbs.S3Service.PutObject(params)
	if err != nil {
		log.Printf("Failed to put object %s into bucket %s with a key of %s\n", pathToFile, asbs.BucketName, fileName)
		log.Println(err)
		return err
	}

	return nil
}

// Get the name to be used for the .zip archive without the .zip extension.
func (asbs *AmazonS3BackupService) ArtifactFilename(jobName string) string {
	return jobName
}

// Get a friendly name for the email template of where this backup was stored. In this case, the name of the S3 bucket.
func (asbs *AmazonS3BackupService) BackupLocation() string {
	return fmt.Sprintf("S3 Bucket: %s", asbs.BucketName)
}

// Create the S3 bucket.
func (asbs *AmazonS3BackupService) createBucket(bucketName string) error {
	asbs.setEnvvars()
	params := &s3.CreateBucketInput{
		Bucket: aws.String(bucketName),
	}

	_, err := asbs.S3Service.CreateBucket(params)
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok {
			if aerr.Code() == ERROR_CODE_INVALID_BUCKET_NAME {
				log.Printf("The specified bucket is not valid. Bucket name: %s\n", bucketName)
				log.Println(err)
				return err
			}
			// If the BucketAlreadyOwnedByYou error is raised then this bucket already exists.
			if aerr.Code() == ERROR_CODE_BUCKET_ALREADY_OWNED_BY_YOU {
				return nil
			}

		}
		return err
	}

	if err = asbs.S3Service.WaitUntilBucketExists(&s3.HeadBucketInput{Bucket: &bucketName}); err != nil {
		log.Printf("Failed to wait for bucket to exist %s, %s\n", bucketName, err)
		return err
	}

	return nil
}

func (asbs *AmazonS3BackupService) putBucketLifecycleConfiguration() error {
	params := &s3.PutBucketLifecycleConfigurationInput{
		Bucket: aws.String(asbs.BucketName),
		LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
			Rules: []*s3.LifecycleRule{
				{
					Prefix: aws.String(""),
					Status: aws.String("Enabled"),
					ID:     aws.String("frosty-backup-retention-policy"),
					Expiration: &s3.LifecycleExpiration{
						Days: &asbs.RetentionDays,
					},
				},
			},
		},
	}

	_, err := asbs.S3Service.PutBucketLifecycleConfiguration(params)
	if err != nil {
		log.Printf("Failed to create bucket lifecycle configuration, %s.", err)
		return err
	}

	return nil
}

// Set the required AWS environment variables.
func (asbs *AmazonS3BackupService) setEnvvars() {
	os.Setenv(ENVVAR_AWS_ACCESS_KEY_ID, asbs.AccessKeyId)
	os.Setenv(ENVVAR_AWS_SECRET_ACCESS_KEY, asbs.SecretAccessKey)
	os.Setenv(ENVVAR_AWS_REGION, asbs.Region)
}

// Get the name to use for the file being stored in S3.
func getObjectKey(fileName string) string {
	hostname, err := os.Hostname()
	if err != nil {
		log.Fatal("Could not determine hostname.", err)
		return ""
	}

	return fmt.Sprintf("%s/%s/%s_%s", hostname, time.Now().Format("20060102"), time.Now().Format("15:04:05"), fileName)
}
<commit_msg>Make the S3 retention period optional. If no retention period is set or the retention period is set to 0 then no policy will be set and any existing policy will be deleted. This allows people to set a policy outside of Frosty if they wish to.<commit_after>package backupservice

import (
	"log"
	"os"

	"fmt"

	"path/filepath"

	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/mleonard87/frosty/config"
)

const (
	BUCKET_NAME                            string = "frosty.backups"
	ERROR_CODE_INVALID_BUCKET_NAME         string = "InvalidBucketName"
	ERROR_CODE_BUCKET_ALREADY_OWNED_BY_YOU string = "BucketAlreadyOwnedByYou"
)

type AmazonS3BackupService struct {
	AccessKeyId     string
	SecretAccessKey string
	Region          string
	AccountId       string
	RetentionDays   int64
	BucketName      string
	S3Service       *s3.S3
}

// Return the backup service type this must match the string as used as the JSON property in the frosty backup config.
func (agss *AmazonS3BackupService) Name() string {
	return config.BACKUP_SERVICE_AMAZON_S3
}

// Initialise any variable needed for backups.
func (asbs *AmazonS3BackupService) SetConfig(backupConfig *config.BackupConfig) {
	asbs.AccessKeyId = backupConfig.BackupConfig["accessKeyId"].(string)
	asbs.SecretAccessKey = backupConfig.BackupConfig["secretAccessKey"].(string)
	asbs.Region = backupConfig.BackupConfig["region"].(string)
	asbs.AccountId = backupConfig.BackupConfig["accountId"].(string)

	// Attempt to get the retentionDays config property. If this can't be found then default to 0.
	// 0 will not set a life cycle policy and any existing policy will remain.
	rd, ok := backupConfig.BackupConfig["retentionDays"]
	if ok {
		asbs.RetentionDays = int64(rd.(float64))
	} else {
		asbs.RetentionDays = 0
	}

	asbs.BucketName = BUCKET_NAME
}

// Initialise anything in the backup service that needs to be created prior to uploading files. In this instance we need
// to create a bucket to store the backups if one does not already exist. This always uses a bucket
// called "frosty.backups".
func (asbs *AmazonS3BackupService) Init() error {
	asbs.setEnvvars()
	asbs.S3Service = s3.New(session.New(), &aws.Config{})

	err := asbs.createBucket(asbs.BucketName)
	if err != nil {
		log.Println("Error creating bucket")
		log.Println(err)
		return err
	}

	err = asbs.putBucketLifecycleConfiguration()
	if err != nil {
		log.Println("Error creating bucket lifecycle")
		log.Println(err)
		return err
	}

	return nil
}

// Store the file in pathToFile in the bucket in S3.
func (asbs *AmazonS3BackupService) StoreFile(pathToFile string) error {
	_, fileName := filepath.Split(pathToFile)

	key := getObjectKey(fileName)

	f, err := os.Open(pathToFile)
	if err != nil {
		log.Printf("Failed to open file to store: %s", pathToFile)
		log.Println(err)
		return err
	}

	params := &s3.PutObjectInput{
		Body:   f,
		Bucket: &asbs.BucketName,
		Key:    &key,
	}

	_, err = asbs.S3Service.PutObject(params)
	if err != nil {
		log.Printf("Failed to put object %s into bucket %s with a key of %s\n", pathToFile, asbs.BucketName, fileName)
		log.Println(err)
		return err
	}

	return nil
}

// Get the name to be used for the .zip archive without the .zip extension.
func (asbs *AmazonS3BackupService) ArtifactFilename(jobName string) string {
	return jobName
}

// Get a friendly name for the email template of where this backup was stored. In this case, the name of the S3 bucket.
func (asbs *AmazonS3BackupService) BackupLocation() string {
	return fmt.Sprintf("S3 Bucket: %s", asbs.BucketName)
}

// Create the S3 bucket.
func (asbs *AmazonS3BackupService) createBucket(bucketName string) error {
	asbs.setEnvvars()
	params := &s3.CreateBucketInput{
		Bucket: aws.String(bucketName),
	}

	_, err := asbs.S3Service.CreateBucket(params)
	if err != nil {
		if aerr, ok := err.(awserr.Error); ok {
			if aerr.Code() == ERROR_CODE_INVALID_BUCKET_NAME {
				log.Printf("The specified bucket is not valid. Bucket name: %s\n", bucketName)
				log.Println(err)
				return err
			}
			// If the BucketAlreadyOwnedByYou error is raised then this bucket already exists.
			if aerr.Code() == ERROR_CODE_BUCKET_ALREADY_OWNED_BY_YOU {
				return nil
			}

		}
		return err
	}

	if err = asbs.S3Service.WaitUntilBucketExists(&s3.HeadBucketInput{Bucket: &bucketName}); err != nil {
		log.Printf("Failed to wait for bucket to exist %s, %s\n", bucketName, err)
		return err
	}

	return nil
}

func (asbs *AmazonS3BackupService) putBucketLifecycleConfiguration() error {
	// If the retention period is not 0 days then submit a new life cycle policy.
	if asbs.RetentionDays != 0 {
		params := &s3.PutBucketLifecycleConfigurationInput{
			Bucket: aws.String(asbs.BucketName),
			LifecycleConfiguration: &s3.BucketLifecycleConfiguration{
				Rules: []*s3.LifecycleRule{
					{
						Prefix: aws.String(""),
						Status: aws.String("Enabled"),
						ID:     aws.String("frosty-backup-retention-policy"),
						Expiration: &s3.LifecycleExpiration{
							Days: &asbs.RetentionDays,
						},
					},
				},
			},
		}

		_, err := asbs.S3Service.PutBucketLifecycleConfiguration(params)
		if err != nil {
			log.Printf("Failed to create bucket lifecycle configuration, %s.\n", err)
			return err
		}
	}

	return nil
}

// Set the required AWS environment variables.
func (asbs *AmazonS3BackupService) setEnvvars() {
	os.Setenv(ENVVAR_AWS_ACCESS_KEY_ID, asbs.AccessKeyId)
	os.Setenv(ENVVAR_AWS_SECRET_ACCESS_KEY, asbs.SecretAccessKey)
	os.Setenv(ENVVAR_AWS_REGION, asbs.Region)
}

// Get the name to use for the file being stored in S3.
func getObjectKey(fileName string) string {
	hostname, err := os.Hostname()
	if err != nil {
		log.Fatal("Could not determine hostname.", err)
		return ""
	}

	return fmt.Sprintf("%s/%s/%s_%s", hostname, time.Now().Format("20060102"), time.Now().Format("15:04:05"), fileName)
}
<|endoftext|>


<commit_before>package canons100

import (
	mwclient "cgt.name/pkg/go-mwclient"
	"cgt.name/pkg/go-mwclient/params"
	"fmt"
	"github.com/antonholmquist/jason"
	"github.com/garyhouston/takenwith/exifcamera"
	"strings"
)

// This module recategorises files that the main bot has dumped into
// "Category:Taken with unidentified Canon PowerShot S100" and
// "Category:Taken with unidentified Canon PowerShot S110".

type CatInfo struct {
	ExifModel         string
	UnidCategory      string
	PowershotCategory string
	IxusCategory      string
}

func moveFile(file string, powershot bool, cat CatInfo, client *mwclient.Client, verbose bool) {
	var target string
	var reason string
	if powershot {
		target = cat.PowershotCategory
		reason = "since Exif contains ISO speed rating"
	} else {
		target = cat.IxusCategory
		reason = "since Exif lacks ISO speed rating"
	}
	if verbose {
		fmt.Println("moving", file, "from", cat.UnidCategory, "to", target)
	}

	// There's a small chance that saving a page may fail due to an
	// edit conflict. It also occasionally fails with
	// "badtoken: Invalid token" for unknown reason. Try up
	// to 3 times before giving up.
	var saveError error
	for i := 0; i < 3; i++ {
		text, timestamp, err := client.GetPageByName(file)
		if err != nil {
			panic(fmt.Sprintf("%v %v", file, err))
		}
		newText := strings.Replace(text, cat.UnidCategory, target, -1)
		editcfg := map[string]string{
			"action":        "edit",
			"title":         file,
			"text":          newText,
			"summary":       "moved from [[" + cat.UnidCategory + "]] to [[" + target + "]] " + reason,
			"minor":         "",
			"bot":           "",
			"basetimestamp": timestamp,
		}
		saveError = client.Edit(editcfg)
		if saveError == nil {
			break
		}
	}
}

func checkSpeedRatings(metadata []*jason.Object) bool {
	for i := 0; i < len(metadata); i++ {
		name, err := metadata[i].GetString("name")
		if err != nil {
			panic(err)
		}
		if name == "ISOSpeedRatings" {
			return true
		}
		if name == "metadata" {
			// MediaWiki can return strange embedded metadata
			// arrays for PNG files.
			// E.g., File:Plaza_in_Front_of_BEXCO.png
			obj, err := metadata[i].GetObjectArray("value")
			// Ignore if not an object array.
			if err == nil && checkSpeedRatings(obj) {
				return true
			}
		}
	}
	return false
}

func processFile(pageObj *jason.Object, cat CatInfo, client *mwclient.Client, verbose bool) {
	title, err := pageObj.GetString("title")
	if err != nil {
		panic(err)
	}
	imageinfo, err := pageObj.GetObjectArray("imageinfo")
	if err == nil {
		_, model := exifcamera.ExtractCamera(imageinfo[0])
		if model != cat.ExifModel {
			fmt.Println(title)
			fmt.Println("Skipping due to wrong model in Exif")
			return
		}
		metadata, err := imageinfo[0].GetObjectArray("commonmetadata")
		if err != nil {
			panic(err)
		}
		if checkSpeedRatings(metadata) {
			moveFile(title, true, cat, client, verbose)
		} else {
			moveFile(title, false, cat, client, verbose)
		}
	}
}

func ProcessCategory(cat CatInfo, client *mwclient.Client, verbose bool) {
	params := params.Values{
		"generator": "categorymembers",
		"gcmtitle":  cat.UnidCategory,
		"gcmtype":   "file",
		"gcmsort":   "sortkey",
		"gcmlimit":  "100", // Maximum files per batch, API allows 5k with bot flag.
		"prop":      "imageinfo",
		"iiprop":    "commonmetadata",
	}
	query := client.NewQuery(params)
	for query.Next() {
		json := query.Resp()
		pages, err := json.GetObject("query", "pages")
		if err != nil {
			// empty category
			return
		}
		pagesMap := pages.Map()
		if len(pagesMap) > 0 {
			for _, page := range pagesMap {
				pageObj, err := page.Object()
				if err != nil {
					panic(err)
				}
				processFile(pageObj, cat, client, verbose)
			}
		}
	}
}
<commit_msg>Terminate on page save error<commit_after>package canons100

import (
	mwclient "cgt.name/pkg/go-mwclient"
	"cgt.name/pkg/go-mwclient/params"
	"fmt"
	"github.com/antonholmquist/jason"
	"github.com/garyhouston/takenwith/exifcamera"
	"strings"
)

// This module recategorises files that the main bot has dumped into
// "Category:Taken with unidentified Canon PowerShot S100" and
// "Category:Taken with unidentified Canon PowerShot S110".

type CatInfo struct {
	ExifModel         string
	UnidCategory      string
	PowershotCategory string
	IxusCategory      string
}

func moveFile(file string, powershot bool, cat CatInfo, client *mwclient.Client, verbose bool) {
	var target string
	var reason string
	if powershot {
		target = cat.PowershotCategory
		reason = "since Exif contains ISO speed rating"
	} else {
		target = cat.IxusCategory
		reason = "since Exif lacks ISO speed rating"
	}
	if verbose {
		fmt.Println("moving", file, "from", cat.UnidCategory, "to", target)
	}

	// There's a small chance that saving a page may fail due to an
	// edit conflict. It also occasionally fails with
	// "badtoken: Invalid token" for unknown reason. Try up
	// to 3 times before giving up.
	var saveError error
	for i := 0; i < 3; i++ {
		text, timestamp, err := client.GetPageByName(file)
		if err != nil {
			panic(fmt.Sprintf("%v %v", file, err))
		}
		newText := strings.Replace(text, cat.UnidCategory, target, -1)
		editcfg := map[string]string{
			"action":        "edit",
			"title":         file,
			"text":          newText,
			"summary":       "moved from [[" + cat.UnidCategory + "]] to [[" + target + "]] " + reason,
			"minor":         "",
			"bot":           "",
			"basetimestamp": timestamp,
		}
		saveError = client.Edit(editcfg)
		if saveError == nil {
			break
		}
	}
	if saveError != nil {
		panic(fmt.Sprintf("Failed to save %v %v", file, saveError))
	}
}

func checkSpeedRatings(metadata []*jason.Object) bool {
	for i := 0; i < len(metadata); i++ {
		name, err := metadata[i].GetString("name")
		if err != nil {
			panic(err)
		}
		if name == "ISOSpeedRatings" {
			return true
		}
		if name == "metadata" {
			// MediaWiki can return strange embedded metadata
			// arrays for PNG files.
			// E.g., File:Plaza_in_Front_of_BEXCO.png
			obj, err := metadata[i].GetObjectArray("value")
			// Ignore if not an object array.
			if err == nil && checkSpeedRatings(obj) {
				return true
			}
		}
	}
	return false
}

func processFile(pageObj *jason.Object, cat CatInfo, client *mwclient.Client, verbose bool) {
	title, err := pageObj.GetString("title")
	if err != nil {
		panic(err)
	}
	imageinfo, err := pageObj.GetObjectArray("imageinfo")
	if err == nil {
		_, model := exifcamera.ExtractCamera(imageinfo[0])
		if model != cat.ExifModel {
			fmt.Println(title)
			fmt.Println("Skipping due to wrong model in Exif")
			return
		}
		metadata, err := imageinfo[0].GetObjectArray("commonmetadata")
		if err != nil {
			panic(err)
		}
		if checkSpeedRatings(metadata) {
			moveFile(title, true, cat, client, verbose)
		} else {
			moveFile(title, false, cat, client, verbose)
		}
	}
}

func ProcessCategory(cat CatInfo, client *mwclient.Client, verbose bool) {
	params := params.Values{
		"generator": "categorymembers",
		"gcmtitle":  cat.UnidCategory,
		"gcmtype":   "file",
		"gcmsort":   "sortkey",
		"gcmlimit":  "100", // Maximum files per batch, API allows 5k with bot flag.
		"prop":      "imageinfo",
		"iiprop":    "commonmetadata",
	}
	query := client.NewQuery(params)
	for query.Next() {
		json := query.Resp()
		pages, err := json.GetObject("query", "pages")
		if err != nil {
			// empty category
			return
		}
		pagesMap := pages.Map()
		if len(pagesMap) > 0 {
			for _, page := range pagesMap {
				pageObj, err := page.Object()
				if err != nil {
					panic(err)
				}
				processFile(pageObj, cat, client, verbose)
			}
		}
	}
}
<|endoftext|>


<commit_before>package capnslog

import (
	"fmt"
	"os"
)

type PackageLogger struct {
	pkg   string
	level LogLevel
}

const calldepth = 3

func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...LogEntry) {
	logger.Lock()
	defer logger.Unlock()
	if logger.formatter != nil {
		logger.formatter.Format(p.pkg, inLevel, depth+1, entries...)
	}
}

func (p *PackageLogger) LevelAt(l LogLevel) bool {
	return p.level >= l
}

// log stdlib compatibility

func (p *PackageLogger) Println(args ...interface{}) {
	if p.level < INFO {
		return
	}
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintln(args...)))
}

func (p *PackageLogger) Printf(format string, args ...interface{}) {
	if p.level < INFO {
		return
	}
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Print(args ...interface{}) {
	if p.level < INFO {
		return
	}
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprint(args...)))
}

// Panic and fatal

func (p *PackageLogger) Panicf(format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	panic(s)
}

func (p *PackageLogger) Panic(args ...interface{}) {
	s := fmt.Sprint(args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	panic(s)
}

func (p *PackageLogger) Fatalf(format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	os.Exit(1)
}

func (p *PackageLogger) Fatal(args ...interface{}) {
	s := fmt.Sprint(args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	os.Exit(1)
}

// Error Functions

func (p *PackageLogger) Errorf(format string, args ...interface{}) {
	if p.level < ERROR {
		return
	}
	p.internalLog(calldepth, ERROR, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Error(entries ...LogEntry) {
	if p.level < ERROR {
		return
	}
	p.internalLog(calldepth, ERROR, entries...)
}

// Warning Functions

func (p *PackageLogger) Warningf(format string, args ...interface{}) {
	if p.level < WARNING {
		return
	}
	p.internalLog(calldepth, WARNING, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Warning(entries ...LogEntry) {
	if p.level < WARNING {
		return
	}
	p.internalLog(calldepth, WARNING, entries...)
}

// Notice Functions

func (p *PackageLogger) Noticef(format string, args ...interface{}) {
	if p.level < NOTICE {
		return
	}
	p.internalLog(calldepth, NOTICE, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Notice(entries ...LogEntry) {
	if p.level < NOTICE {
		return
	}
	p.internalLog(calldepth, NOTICE, entries...)
}

// Info Functions

func (p *PackageLogger) Infof(format string, args ...interface{}) {
	if p.level < INFO {
		return
	}
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Info(entries ...LogEntry) {
	if p.level < INFO {
		return
	}
	p.internalLog(calldepth, INFO, entries...)
}

// Debug Functions

func (p *PackageLogger) Debugf(format string, args ...interface{}) {
	if p.level < DEBUG {
		return
	}
	p.internalLog(calldepth, DEBUG, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Debug(entries ...LogEntry) {
	if p.level < DEBUG {
		return
	}
	p.internalLog(calldepth, DEBUG, entries...)
}

// Trace Functions

func (p *PackageLogger) Tracef(format string, args ...interface{}) {
	if p.level < TRACE {
		return
	}
	p.internalLog(calldepth, TRACE, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Trace(entries ...LogEntry) {
	if p.level < TRACE {
		return
	}
	p.internalLog(calldepth, TRACE, entries...)
}

func (p *PackageLogger) Flush() {
	logger.Lock()
	defer logger.Unlock()
	logger.formatter.Flush()
}
<commit_msg>capnslog: consolidate unnecessarily duplicated LogLevel filter checks<commit_after>package capnslog

import (
	"fmt"
	"os"
)

type PackageLogger struct {
	pkg   string
	level LogLevel
}

const calldepth = 3

func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...LogEntry) {
	if inLevel != CRITICAL && p.level < inLevel {
		return
	}
	logger.Lock()
	defer logger.Unlock()
	if logger.formatter != nil {
		logger.formatter.Format(p.pkg, inLevel, depth+1, entries...)
	}
}

func (p *PackageLogger) LevelAt(l LogLevel) bool {
	return p.level >= l
}

// log stdlib compatibility

func (p *PackageLogger) Println(args ...interface{}) {
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintln(args...)))
}

func (p *PackageLogger) Printf(format string, args ...interface{}) {
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Print(args ...interface{}) {
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprint(args...)))
}

// Panic and fatal

func (p *PackageLogger) Panicf(format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	panic(s)
}

func (p *PackageLogger) Panic(args ...interface{}) {
	s := fmt.Sprint(args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	panic(s)
}

func (p *PackageLogger) Fatalf(format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	os.Exit(1)
}

func (p *PackageLogger) Fatal(args ...interface{}) {
	s := fmt.Sprint(args...)
	p.internalLog(calldepth, CRITICAL, BaseLogEntry(s))
	os.Exit(1)
}

// Error Functions

func (p *PackageLogger) Errorf(format string, args ...interface{}) {
	p.internalLog(calldepth, ERROR, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Error(entries ...LogEntry) {
	p.internalLog(calldepth, ERROR, entries...)
}

// Warning Functions

func (p *PackageLogger) Warningf(format string, args ...interface{}) {
	p.internalLog(calldepth, WARNING, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Warning(entries ...LogEntry) {
	p.internalLog(calldepth, WARNING, entries...)
}

// Notice Functions

func (p *PackageLogger) Noticef(format string, args ...interface{}) {
	p.internalLog(calldepth, NOTICE, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Notice(entries ...LogEntry) {
	p.internalLog(calldepth, NOTICE, entries...)
}

// Info Functions

func (p *PackageLogger) Infof(format string, args ...interface{}) {
	p.internalLog(calldepth, INFO, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Info(entries ...LogEntry) {
	p.internalLog(calldepth, INFO, entries...)
}

// Debug Functions

func (p *PackageLogger) Debugf(format string, args ...interface{}) {
	p.internalLog(calldepth, DEBUG, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Debug(entries ...LogEntry) {
	p.internalLog(calldepth, DEBUG, entries...)
}

// Trace Functions

func (p *PackageLogger) Tracef(format string, args ...interface{}) {
	p.internalLog(calldepth, TRACE, BaseLogEntry(fmt.Sprintf(format, args...)))
}

func (p *PackageLogger) Trace(entries ...LogEntry) {
	p.internalLog(calldepth, TRACE, entries...)
}

func (p *PackageLogger) Flush() {
	logger.Lock()
	defer logger.Unlock()
	logger.formatter.Flush()
}
<|endoftext|>


<commit_before>/*
Copyright 2018 The Doctl 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 commands

import (
	"fmt"
	"io/ioutil"

	"github.com/digitalocean/doctl"
	"github.com/digitalocean/doctl/commands/displayers"
	"github.com/digitalocean/doctl/do"
	"github.com/digitalocean/godo"
	"github.com/spf13/cobra"
	"golang.org/x/crypto/ssh"
)

// SSHKeys creates the ssh key commands hierarchy.
func SSHKeys() *Command {
	cmd := &Command{
		Command: &cobra.Command{
			Use:     "ssh-key",
			Aliases: []string{"k"},
			Short:   "Provides commands that manage SSH keys on your account",
			Long:    `The sub-commands of 'doctl compute ssh-key' manage the SSH keys on your account.

DigitalOcean allows you to add SSH public keys to the interface so that you can embed your public key into a Droplet at the time of creation. Only the public key is required to take advantage of this functionality. Note that this command does not add, delete, or otherwise modify any ssh keys that may be on existing Droplets.`,
		},
	}

	CmdBuilderWithDocs(cmd, RunKeyList, "list", "list all ssh keys",`Use this command to list the id, fingerprint, public_key, and name of all SSH keys on your account.`, Writer,
		aliasOpt("ls"), displayerType(&displayers.Key{}))

	CmdBuilderWithDocs(cmd, RunKeyGet, "get <key-id|key-fingerprint>", "retrieve ssh key",`Use this command to get the id, fingerprint, public_key, and name of a specific SSH key on your account.`, Writer,
		aliasOpt("g"), displayerType(&displayers.Key{}))

	cmdSSHKeysCreate := CmdBuilderWithDocs(cmd, RunKeyCreate, "create <key-name>", "add an ssh key",`Use this command to add a new SSH key to your account.

Set the "name" attribute to the name you wish to use and the "public_key" attribute to a string of the full public key you are adding.
Note that this command will not add an ssh key to any existing Droplets.`, Writer,
		aliasOpt("c"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysCreate, doctl.ArgKeyPublicKey, "", "", "Key contents", requiredOpt())

	cmdSSHKeysImport := CmdBuilderWithDocs(cmd, RunKeyImport, "import <key-name>", "import an ssh key",`Use this command to add a new SSH key to your account, using a local public key file.

Note that this command will not add an ssh key to any existing Droplets`, Writer,
		aliasOpt("i"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysImport, doctl.ArgKeyPublicKeyFile, "", "", "Public key file", requiredOpt())

	cmdRunKeyDelete := CmdBuilderWithDocs(cmd, RunKeyDelete, "delete <key-id|key-fingerprint>", "delete an ssh key",`Use this command to delete an ssh key from your account.

Note that this does not delete an ssh key from any Droplets.`, Writer,
		aliasOpt("d"))
	AddBoolFlag(cmdRunKeyDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force ssh key delete")

	cmdSSHKeysUpdate := CmdBuilderWithDocs(cmd, RunKeyUpdate, "update <key-id|key-fingerprint>", "update an ssh key",`Use this command to update the name of an ssh key on your account.`, Writer,
		aliasOpt("u"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysUpdate, doctl.ArgKeyName, "", "", "Key name", requiredOpt())

	return cmd
}

// RunKeyList lists keys.
func RunKeyList(c *CmdConfig) error {
	ks := c.Keys()

	list, err := ks.List()
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: list}
	return c.Display(item)
}

// RunKeyGet retrieves a key.
func RunKeyGet(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	rawKey := c.Args[0]
	k, err := ks.Get(rawKey)

	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*k}}
	return c.Display(item)
}

// RunKeyCreate uploads a SSH key.
func RunKeyCreate(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	name := c.Args[0]

	publicKey, err := c.Doit.GetString(c.NS, doctl.ArgKeyPublicKey)
	if err != nil {
		return err
	}

	kcr := &godo.KeyCreateRequest{
		Name:      name,
		PublicKey: publicKey,
	}

	r, err := ks.Create(kcr)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*r}}
	return c.Display(item)
}

// RunKeyImport imports a key from a file
func RunKeyImport(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	keyPath, err := c.Doit.GetString(c.NS, doctl.ArgKeyPublicKeyFile)
	if err != nil {
		return err
	}

	keyName := c.Args[0]

	keyFile, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return err
	}

	_, comment, _, _, err := ssh.ParseAuthorizedKey(keyFile)
	if err != nil {
		return err
	}

	if len(keyName) < 1 {
		keyName = comment
	}

	kcr := &godo.KeyCreateRequest{
		Name:      keyName,
		PublicKey: string(keyFile),
	}

	r, err := ks.Create(kcr)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*r}}
	return c.Display(item)
}

// RunKeyDelete deletes a key.
func RunKeyDelete(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
	if err != nil {
		return nil
	}

	if force || AskForConfirm("delete ssh key") == nil {
		rawKey := c.Args[0]
		return ks.Delete(rawKey)
	}

	return fmt.Errorf("operation aborted")
}

// RunKeyUpdate updates a key.
func RunKeyUpdate(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	rawKey := c.Args[0]

	name, err := c.Doit.GetString(c.NS, doctl.ArgKeyName)
	if err != nil {
		return err
	}

	req := &godo.KeyUpdateRequest{
		Name: name,
	}

	k, err := ks.Update(rawKey, req)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*k}}
	return c.Display(item)
}
<commit_msg>update short descriptions, sshkeys.go<commit_after>/*
Copyright 2018 The Doctl 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 commands

import (
	"fmt"
	"io/ioutil"

	"github.com/digitalocean/doctl"
	"github.com/digitalocean/doctl/commands/displayers"
	"github.com/digitalocean/doctl/do"
	"github.com/digitalocean/godo"
	"github.com/spf13/cobra"
	"golang.org/x/crypto/ssh"
)

// SSHKeys creates the ssh key commands hierarchy.
func SSHKeys() *Command {
	cmd := &Command{
		Command: &cobra.Command{
			Use:     "ssh-key",
			Aliases: []string{"k"},
			Short:   "Provides commands that manage SSH keys on your account",
			Long:    `The sub-commands of 'doctl compute ssh-key' manage the SSH keys on your account.

DigitalOcean allows you to add SSH public keys to the interface so that you can embed your public key into a Droplet at the time of creation. Only the public key is required to take advantage of this functionality. Note that this command does not add, delete, or otherwise modify any ssh keys that may be on existing Droplets.`,
		},
	}

	CmdBuilderWithDocs(cmd, RunKeyList, "list", "List all SSH keys on your account",`Use this command to list the id, fingerprint, public_key, and name of all SSH keys on your account.`, Writer,
		aliasOpt("ls"), displayerType(&displayers.Key{}))

	CmdBuilderWithDocs(cmd, RunKeyGet, "get <key-id|key-fingerprint>", "Retrive information about an SSH key on your account",`Use this command to get the id, fingerprint, public_key, and name of a specific SSH key on your account.`, Writer,
		aliasOpt("g"), displayerType(&displayers.Key{}))

	cmdSSHKeysCreate := CmdBuilderWithDocs(cmd, RunKeyCreate, "create <key-name>", "Create a new SSH key on your account",`Use this command to add a new SSH key to your account.

Set the "name" attribute to the name you wish to use and the "public_key" attribute to a string of the full public key you are adding.
Note that this command will not add an ssh key to any existing Droplets.`, Writer,
		aliasOpt("c"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysCreate, doctl.ArgKeyPublicKey, "", "", "Key contents", requiredOpt())

	cmdSSHKeysImport := CmdBuilderWithDocs(cmd, RunKeyImport, "import <key-name>", "Import an SSH key from your computer to your account",`Use this command to add a new SSH key to your account, using a local public key file.

Note that this command will not add an ssh key to any existing Droplets`, Writer,
		aliasOpt("i"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysImport, doctl.ArgKeyPublicKeyFile, "", "", "Public key file", requiredOpt())

	cmdRunKeyDelete := CmdBuilderWithDocs(cmd, RunKeyDelete, "delete <key-id|key-fingerprint>", "Permanently delete an SSH key from your account",`Use this command to permanently delete an ssh key from your account.

Note that this does not delete an ssh key from any Droplets.`, Writer,
		aliasOpt("d"))
	AddBoolFlag(cmdRunKeyDelete, doctl.ArgForce, doctl.ArgShortForce, false, "Force ssh key delete")

	cmdSSHKeysUpdate := CmdBuilderWithDocs(cmd, RunKeyUpdate, "update <key-id|key-fingerprint>", "Update an SSH key's name",`Use this command to update the name of an ssh key on your account.`, Writer,
		aliasOpt("u"), displayerType(&displayers.Key{}))
	AddStringFlag(cmdSSHKeysUpdate, doctl.ArgKeyName, "", "", "Key name", requiredOpt())

	return cmd
}

// RunKeyList lists keys.
func RunKeyList(c *CmdConfig) error {
	ks := c.Keys()

	list, err := ks.List()
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: list}
	return c.Display(item)
}

// RunKeyGet retrieves a key.
func RunKeyGet(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	rawKey := c.Args[0]
	k, err := ks.Get(rawKey)

	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*k}}
	return c.Display(item)
}

// RunKeyCreate uploads a SSH key.
func RunKeyCreate(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	name := c.Args[0]

	publicKey, err := c.Doit.GetString(c.NS, doctl.ArgKeyPublicKey)
	if err != nil {
		return err
	}

	kcr := &godo.KeyCreateRequest{
		Name:      name,
		PublicKey: publicKey,
	}

	r, err := ks.Create(kcr)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*r}}
	return c.Display(item)
}

// RunKeyImport imports a key from a file
func RunKeyImport(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	keyPath, err := c.Doit.GetString(c.NS, doctl.ArgKeyPublicKeyFile)
	if err != nil {
		return err
	}

	keyName := c.Args[0]

	keyFile, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return err
	}

	_, comment, _, _, err := ssh.ParseAuthorizedKey(keyFile)
	if err != nil {
		return err
	}

	if len(keyName) < 1 {
		keyName = comment
	}

	kcr := &godo.KeyCreateRequest{
		Name:      keyName,
		PublicKey: string(keyFile),
	}

	r, err := ks.Create(kcr)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*r}}
	return c.Display(item)
}

// RunKeyDelete deletes a key.
func RunKeyDelete(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	force, err := c.Doit.GetBool(c.NS, doctl.ArgForce)
	if err != nil {
		return nil
	}

	if force || AskForConfirm("delete ssh key") == nil {
		rawKey := c.Args[0]
		return ks.Delete(rawKey)
	}

	return fmt.Errorf("operation aborted")
}

// RunKeyUpdate updates a key.
func RunKeyUpdate(c *CmdConfig) error {
	ks := c.Keys()

	if len(c.Args) != 1 {
		return doctl.NewMissingArgsErr(c.NS)
	}

	rawKey := c.Args[0]

	name, err := c.Doit.GetString(c.NS, doctl.ArgKeyName)
	if err != nil {
		return err
	}

	req := &godo.KeyUpdateRequest{
		Name: name,
	}

	k, err := ks.Update(rawKey, req)
	if err != nil {
		return err
	}

	item := &displayers.Key{Keys: do.SSHKeys{*k}}
	return c.Display(item)
}
<|endoftext|>