<commit_before>// Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
// Copyright (c) 2006-2014 Sippy Software, Inc. All rights reserved.
// Copyright (c) 2016 Andriy Pylypenko. All rights reserved.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package sippy

import (
    "crypto/rand"
    "fmt"
    "net"
    "time"
    "strings"
    "sync"

    "sippy/conf"
    "sippy/math"
    "sippy/net"
    "sippy/time"
    "sippy/types"
    "sippy/utils"
)

type Rtp_proxy_client_udp struct {
    address             net.Addr
    uopts               *udpServerOpts
    pending_requests    map[string]*rtpp_req_udp
    global_config       sippy_conf.Config
    delay_flt           sippy_math.RecFilter
    worker              *udpServer
    hostport            *sippy_net.HostPort
    lock                sync.Mutex
    owner               sippy_types.RtpProxyClient
}

type rtpp_req_udp struct {
    next_retr       float64
    triesleft       int64
    timer           *Timeout
    command         string
    result_callback func(string)
    stime           *sippy_time.MonoTime
    retransmits     int
}

func new_rtpp_req_udp(next_retr float64, triesleft int64, timer *Timeout, command string, result_callback func(string)) *rtpp_req_udp {
    stime, _ := sippy_time.NewMonoTime()
    return &rtpp_req_udp{
        next_retr       : next_retr,
        triesleft       : triesleft,
        timer           : timer,
        command         : command,
        result_callback : result_callback,
        stime           : stime,
        retransmits     : 0,
    }
}

func getnretrans(first_retr, timeout float64) (int64, error) {
    if first_retr < 0 {
        return 0, fmt.Errorf("getnretrans(%f, %f)", first_retr, timeout)
    }
    var n int64 = 0
    for {
        timeout -= first_retr
        if timeout < 0 {
            break
        }
        first_retr *= 2.0
        n += 1
    }
    return n, nil
}

func newRtp_proxy_client_udp(owner sippy_types.RtpProxyClient, global_config sippy_conf.Config, address net.Addr) (rtp_proxy_transport, error) {
    var err error
    var laddress *sippy_net.HostPort

    self := &Rtp_proxy_client_udp{
        owner               : owner,
        address             : address,
        pending_requests    : make(map[string]*rtpp_req_udp),
        global_config       : global_config,
        delay_flt           : sippy_math.NewRecFilter(0.95, 0.25),
    }
    self.hostport, err = sippy_net.NewHostPortFromAddr(self.address)
    if err != nil {
        return nil, err
    }
    if self.hostport.Host.String()[0] == '[' {
        if self.hostport.Host.String() == "[::1]" {
            laddress = sippy_net.NewHostPort("[::1]", "0")
        } else {
            laddress = sippy_net.NewHostPort("[::]", "0")
        }
    } else {
        if strings.HasPrefix(self.hostport.Host.String(), "127.") {
            laddress = sippy_net.NewHostPort("127.0.0.1", "0")
        } else {
            laddress = sippy_net.NewHostPort("0.0.0.0", "0")
        }
    }
    self.uopts = NewUdpServerOpts(laddress, self.process_reply)
    //self.uopts.ploss_out_rate = self.ploss_out_rate
    //self.uopts.pdelay_out_max = self.pdelay_out_max
    if owner.GetOpts().GetNWorkers() != nil {
        self.uopts.nworkers = *owner.GetOpts().GetNWorkers()
    }
    self.worker, err = NewUdpServer(global_config, self.uopts)
    return self, err
}

func (*Rtp_proxy_client_udp) is_local() bool {
    return false
}

func (self *Rtp_proxy_client_udp) send_command(command string, result_callback func(string)) {
    buf := make([]byte, 16)
    rand.Read(buf)
    cookie := fmt.Sprintf("%x", buf)
    next_retr := self.delay_flt.GetLastval() * 4.0
    exp_time := 3.0
    if command[0] == 'I' {
        exp_time = 10.0
    } else if command[0] == 'G' {
        exp_time = 1.0
    }
    nretr, err := getnretrans(next_retr, exp_time)
    if err != nil {
        self.global_config.ErrorLogger().Debug("getnretrans error: " + err.Error())
        return
    }
    command = cookie + " " + command
    timer := StartTimeout(func() { self.retransmit(cookie) }, nil, time.Duration(next_retr * float64(time.Second)), 1, self.global_config.ErrorLogger())
    preq := new_rtpp_req_udp(next_retr, nretr - 1, timer, command, result_callback)
    self.worker.SendTo([]byte(command), self.hostport)
    self.lock.Lock()
    self.pending_requests[cookie] = preq
    self.lock.Unlock()
}

func (self *Rtp_proxy_client_udp) retransmit(cookie string) {
    self.lock.Lock()
    req, ok := self.pending_requests[cookie]
    if ! ok {
        self.lock.Unlock()
        return
    }
    if req.triesleft <= 0 || self.worker == nil {
        delete(self.pending_requests, cookie)
        self.lock.Unlock()
        self.owner.GoOffline()
        if req.result_callback != nil {
            sippy_utils.SafeCall(func() { req.result_callback("") }, nil/*lock*/, self.global_config.ErrorLogger())
        }
        return
    }
    self.lock.Unlock()
    req.next_retr *= 2
    req.retransmits += 1
    req.timer = StartTimeout(func() { self.retransmit(cookie) }, nil, time.Duration(req.next_retr * float64(time.Second)), 1, self.global_config.ErrorLogger())
    req.stime, _ = sippy_time.NewMonoTime()
    self.worker.SendTo([]byte(req.command), self.hostport)
    req.triesleft -= 1
}

func (self *Rtp_proxy_client_udp) process_reply(data []byte, address *sippy_net.HostPort, worker sippy_net.Transport, rtime *sippy_time.MonoTime) {
    arr := sippy_utils.FieldsN(string(data), 2)
    if len(arr) != 2 {
        self.global_config.ErrorLogger().Debug("Rtp_proxy_client_udp.process_reply(): invalid response " + string(data))
        return
    }
    cookie, result := arr[0], arr[1]
    self.lock.Lock()
    req, ok := self.pending_requests[cookie]
    self.lock.Unlock()
    if ! ok {
        return
    }
    req.timer.Cancel()
    if req.result_callback != nil {
        sippy_utils.SafeCall(func() { req.result_callback(strings.TrimSpace(result)) }, nil/*lock*/, self.global_config.ErrorLogger())
    }
    if req.retransmits == 0 {
        // When we had to do retransmit it is not possible to figure out whether
        // or not this reply is related to the original request or one of the
        // retransmits. Therefore, using it to estimate delay could easily produce
        // bogus value that is too low or even negative if we cook up retransmit
        // while the original response is already in the queue waiting to be
        // processed. This should not be a big issue since UDP command channel does
        // not work very well if the packet loss goes to more than 30-40%.
        self.delay_flt.Apply(rtime.Sub(req.stime).Seconds())
        //print "Rtp_proxy_client_udp.process_reply(): delay %f" % (rtime - stime)
    }
}
/*
    def reconnect(self, address, bind_address = nil):
        self.address = address
        if bind_address != self.uopts.laddress:
            self.uopts.laddress = bind_address
            self.worker.shutdown()
            self.worker = Udp_server(self.global_config, self.uopts)
            self.delay_flt = recfilter(0.95, 0.25)
*/
func (self *Rtp_proxy_client_udp) shutdown() {
    self.worker.Shutdown()
    self.worker = nil
}
/*
    def get_rtpc_delay(self):
        return self.delay_flt.lastval

class selftest(object):
    def gotreply(self, *args):
        from twisted.internet import reactor
        print args
        reactor.crash()

    def run(self):
        import os
        from twisted.internet import reactor
        global_config = {}
        global_config["my_pid"] = os.getpid()
        rtpc = Rtp_proxy_client_udp(global_config, ("127.0.0.1", 22226), nil)
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("Ib", self.gotreply)
        reactor.run()
        rtpc.reconnect(("localhost", 22226), ("0.0.0.0", 34222))
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("V", self.gotreply)
        reactor.run()
        rtpc.reconnect(("localhost", 22226), ("127.0.0.1", 57535))
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("V", self.gotreply)
        reactor.run()
        rtpc.shutdown()

if __name__ == "__main__":
    selftest().run()
*/
<commit_msg>Do not forget to clean up the pending request.<commit_after>// Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
// Copyright (c) 2006-2014 Sippy Software, Inc. All rights reserved.
// Copyright (c) 2016 Andriy Pylypenko. All rights reserved.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package sippy

import (
    "crypto/rand"
    "fmt"
    "net"
    "time"
    "strings"
    "sync"

    "sippy/conf"
    "sippy/math"
    "sippy/net"
    "sippy/time"
    "sippy/types"
    "sippy/utils"
)

type Rtp_proxy_client_udp struct {
    address             net.Addr
    uopts               *udpServerOpts
    pending_requests    map[string]*rtpp_req_udp
    global_config       sippy_conf.Config
    delay_flt           sippy_math.RecFilter
    worker              *udpServer
    hostport            *sippy_net.HostPort
    lock                sync.Mutex
    owner               sippy_types.RtpProxyClient
}

type rtpp_req_udp struct {
    next_retr       float64
    triesleft       int64
    timer           *Timeout
    command         string
    result_callback func(string)
    stime           *sippy_time.MonoTime
    retransmits     int
}

func new_rtpp_req_udp(next_retr float64, triesleft int64, timer *Timeout, command string, result_callback func(string)) *rtpp_req_udp {
    stime, _ := sippy_time.NewMonoTime()
    return &rtpp_req_udp{
        next_retr       : next_retr,
        triesleft       : triesleft,
        timer           : timer,
        command         : command,
        result_callback : result_callback,
        stime           : stime,
        retransmits     : 0,
    }
}

func getnretrans(first_retr, timeout float64) (int64, error) {
    if first_retr < 0 {
        return 0, fmt.Errorf("getnretrans(%f, %f)", first_retr, timeout)
    }
    var n int64 = 0
    for {
        timeout -= first_retr
        if timeout < 0 {
            break
        }
        first_retr *= 2.0
        n += 1
    }
    return n, nil
}

func newRtp_proxy_client_udp(owner sippy_types.RtpProxyClient, global_config sippy_conf.Config, address net.Addr) (rtp_proxy_transport, error) {
    var err error
    var laddress *sippy_net.HostPort

    self := &Rtp_proxy_client_udp{
        owner               : owner,
        address             : address,
        pending_requests    : make(map[string]*rtpp_req_udp),
        global_config       : global_config,
        delay_flt           : sippy_math.NewRecFilter(0.95, 0.25),
    }
    self.hostport, err = sippy_net.NewHostPortFromAddr(self.address)
    if err != nil {
        return nil, err
    }
    if self.hostport.Host.String()[0] == '[' {
        if self.hostport.Host.String() == "[::1]" {
            laddress = sippy_net.NewHostPort("[::1]", "0")
        } else {
            laddress = sippy_net.NewHostPort("[::]", "0")
        }
    } else {
        if strings.HasPrefix(self.hostport.Host.String(), "127.") {
            laddress = sippy_net.NewHostPort("127.0.0.1", "0")
        } else {
            laddress = sippy_net.NewHostPort("0.0.0.0", "0")
        }
    }
    self.uopts = NewUdpServerOpts(laddress, self.process_reply)
    //self.uopts.ploss_out_rate = self.ploss_out_rate
    //self.uopts.pdelay_out_max = self.pdelay_out_max
    if owner.GetOpts().GetNWorkers() != nil {
        self.uopts.nworkers = *owner.GetOpts().GetNWorkers()
    }
    self.worker, err = NewUdpServer(global_config, self.uopts)
    return self, err
}

func (*Rtp_proxy_client_udp) is_local() bool {
    return false
}

func (self *Rtp_proxy_client_udp) send_command(command string, result_callback func(string)) {
    buf := make([]byte, 16)
    rand.Read(buf)
    cookie := fmt.Sprintf("%x", buf)
    next_retr := self.delay_flt.GetLastval() * 4.0
    exp_time := 3.0
    if command[0] == 'I' {
        exp_time = 10.0
    } else if command[0] == 'G' {
        exp_time = 1.0
    }
    nretr, err := getnretrans(next_retr, exp_time)
    if err != nil {
        self.global_config.ErrorLogger().Debug("getnretrans error: " + err.Error())
        return
    }
    command = cookie + " " + command
    timer := StartTimeout(func() { self.retransmit(cookie) }, nil, time.Duration(next_retr * float64(time.Second)), 1, self.global_config.ErrorLogger())
    preq := new_rtpp_req_udp(next_retr, nretr - 1, timer, command, result_callback)
    self.worker.SendTo([]byte(command), self.hostport)
    self.lock.Lock()
    self.pending_requests[cookie] = preq
    self.lock.Unlock()
}

func (self *Rtp_proxy_client_udp) retransmit(cookie string) {
    self.lock.Lock()
    req, ok := self.pending_requests[cookie]
    if ! ok {
        self.lock.Unlock()
        return
    }
    if req.triesleft <= 0 || self.worker == nil {
        delete(self.pending_requests, cookie)
        self.lock.Unlock()
        self.owner.GoOffline()
        if req.result_callback != nil {
            sippy_utils.SafeCall(func() { req.result_callback("") }, nil/*lock*/, self.global_config.ErrorLogger())
        }
        return
    }
    self.lock.Unlock()
    req.next_retr *= 2
    req.retransmits += 1
    req.timer = StartTimeout(func() { self.retransmit(cookie) }, nil, time.Duration(req.next_retr * float64(time.Second)), 1, self.global_config.ErrorLogger())
    req.stime, _ = sippy_time.NewMonoTime()
    self.worker.SendTo([]byte(req.command), self.hostport)
    req.triesleft -= 1
}

func (self *Rtp_proxy_client_udp) process_reply(data []byte, address *sippy_net.HostPort, worker sippy_net.Transport, rtime *sippy_time.MonoTime) {
    arr := sippy_utils.FieldsN(string(data), 2)
    if len(arr) != 2 {
        self.global_config.ErrorLogger().Debug("Rtp_proxy_client_udp.process_reply(): invalid response " + string(data))
        return
    }
    cookie, result := arr[0], arr[1]
    self.lock.Lock()
    req, ok := self.pending_requests[cookie]
    delete(self.pending_requests, cookie)
    self.lock.Unlock()
    if ! ok {
        return
    }
    req.timer.Cancel()
    if req.result_callback != nil {
        sippy_utils.SafeCall(func() { req.result_callback(strings.TrimSpace(result)) }, nil/*lock*/, self.global_config.ErrorLogger())
    }
    if req.retransmits == 0 {
        // When we had to do retransmit it is not possible to figure out whether
        // or not this reply is related to the original request or one of the
        // retransmits. Therefore, using it to estimate delay could easily produce
        // bogus value that is too low or even negative if we cook up retransmit
        // while the original response is already in the queue waiting to be
        // processed. This should not be a big issue since UDP command channel does
        // not work very well if the packet loss goes to more than 30-40%.
        self.delay_flt.Apply(rtime.Sub(req.stime).Seconds())
        //print "Rtp_proxy_client_udp.process_reply(): delay %f" % (rtime - stime)
    }
}
/*
    def reconnect(self, address, bind_address = nil):
        self.address = address
        if bind_address != self.uopts.laddress:
            self.uopts.laddress = bind_address
            self.worker.shutdown()
            self.worker = Udp_server(self.global_config, self.uopts)
            self.delay_flt = recfilter(0.95, 0.25)
*/
func (self *Rtp_proxy_client_udp) shutdown() {
    self.worker.Shutdown()
    self.worker = nil
}
/*
    def get_rtpc_delay(self):
        return self.delay_flt.lastval

class selftest(object):
    def gotreply(self, *args):
        from twisted.internet import reactor
        print args
        reactor.crash()

    def run(self):
        import os
        from twisted.internet import reactor
        global_config = {}
        global_config["my_pid"] = os.getpid()
        rtpc = Rtp_proxy_client_udp(global_config, ("127.0.0.1", 22226), nil)
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("Ib", self.gotreply)
        reactor.run()
        rtpc.reconnect(("localhost", 22226), ("0.0.0.0", 34222))
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("V", self.gotreply)
        reactor.run()
        rtpc.reconnect(("localhost", 22226), ("127.0.0.1", 57535))
        os.system("sockstat | grep -w %d" % global_config["my_pid"])
        rtpc.send_command("V", self.gotreply)
        reactor.run()
        rtpc.shutdown()

if __name__ == "__main__":
    selftest().run()
*/
<|endoftext|>


<commit_before>/*
** Copyright [2013-2016] [Megam Systems]
**
** 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 ubuntu

import (
	"fmt"
	"github.com/megamsys/libmegdc/templates"
	"github.com/megamsys/urknall"
	//"github.com/megamsys/libgo/cmd"
)

const (
	Bridgename = "Bridgename"
	Dnsnames   = "Dnsnames"
	PhyDev     = "PhyDev"
	Network    = "Network"
	Netmask    = "Netmask"
	Gateway    = "Gateway"
	Host       = "Host"
	Interface  = `auto lo
iface lo inet loopback

auto %s
auto %s
iface %s inet static
  address %s
	network %s
  netmask %s
  gateway %s
bridge_ports %s
dns-nameservers %s
source /etc/network/interfaces.d/*.cfg`
)

var ubuntucreatebridge *UbuntuCreateBridge

func init() {
	ubuntucreatebridge = &UbuntuCreateBridge{}
	templates.Register("UbuntuCreateBridge", ubuntucreatebridge)
}

type UbuntuCreateBridge struct {
	bridgename string
	phydev     string
	network    string
	netmask    string
	gateway    string
	dnsnames   string
	host       string
}

func (tpl *UbuntuCreateBridge) Options(t *templates.Template) {
	if bridgename, ok := t.Options[Bridgename]; ok {
		tpl.bridgename = bridgename
	}
	if phydev, ok := t.Options[PhyDev]; ok {
		tpl.phydev = phydev
	}
	if network, ok := t.Options[Network]; ok {
		tpl.network = network
	}
	if netmask, ok := t.Options[Netmask]; ok {
		tpl.netmask = netmask
	}
	if gateway, ok := t.Options[Gateway]; ok {
		tpl.gateway = gateway
	}
	if dnsnames, ok := t.Options[Dnsnames]; ok {
		tpl.dnsnames = dnsnames
	}
	if host, ok := t.Options[Host]; ok {
		tpl.host = host
	}
}

func (tpl *UbuntuCreateBridge) Render(p urknall.Package) {
	p.AddTemplate("bridge", &UbuntuCreateBridgeTemplate{
		bridgename: tpl.bridgename,
		phydev:     tpl.phydev,
		network:    tpl.network,
		netmask:    tpl.netmask,
		gateway:    tpl.gateway,
		dnsnames:   tpl.dnsnames,
		host:       tpl.host,
	})
}

func (tpl *UbuntuCreateBridge) Run(target urknall.Target, inputs []string) error {
	return urknall.Run(target, tpl, inputs)
}

type UbuntuCreateBridgeTemplate struct {
	bridgename string
	phydev     string
	network    string
	netmask    string
	gateway    string
	dnsnames   string
	host       string
}

func (m *UbuntuCreateBridgeTemplate) Render(pkg urknall.Package) {
	var dnsnames string
	ip := m.host
	bridgename := m.bridgename
	phydev := m.phydev
	network := m.network
	netmask := m.netmask
	gateway := m.gateway

  if m.phydev == "" {
		phydev = "eth0"
	}

	if m.bridgename == "" {
		bridgename = "one"
	}
	if m.dnsnames == "" {
		dnsnames = "8.8.8.8 8.8.4.4"
	} else {
		dnsnames = m.dnsnames
	}

	pkg.AddCommands("bridgeutils",
		Shell("apt-get install -y bridge-utils"),
	)
	pkg.AddCommands("interfaces",
		Shell("cp /etc/network/interfaces /etc/network/bkinterfaces"),
		WriteFile("/etc/network/interfaces", fmt.Sprintf(Interface,phydev, bridgename, bridgename, ip,network, netmask, gateway, phydev, dnsnames), "root", 0644),
	)
	pkg.AddCommands("create-bridge",
		Shell("brctl addbr "+bridgename+""),
	)
	pkg.AddCommands("list-bridge",
		Shell("brctl show"),
	)

}
<commit_msg>alignments<commit_after>/*
** Copyright [2013-2016] [Megam Systems]
**
** 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 ubuntu

import (
	"fmt"
	"github.com/megamsys/libmegdc/templates"
	"github.com/megamsys/urknall"
	//"github.com/megamsys/libgo/cmd"
)

const (
	Bridgename = "Bridgename"
	Dnsnames   = "Dnsnames"
	PhyDev     = "PhyDev"
	Network    = "Network"
	Netmask    = "Netmask"
	Gateway    = "Gateway"
	Host       = "Host"
	Interface  = `auto lo
iface lo inet loopback

auto %s
auto %s
iface %s inet static
  address %s
	network %s
  netmask %s
  gateway %s
bridge_ports %s
dns-nameservers %s
source /etc/network/interfaces.d/*.cfg`
)

var ubuntucreatebridge *UbuntuCreateBridge

func init() {
	ubuntucreatebridge = &UbuntuCreateBridge{}
	templates.Register("UbuntuCreateBridge", ubuntucreatebridge)
}

type UbuntuCreateBridge struct {
	bridgename string
	phydev     string
	network    string
	netmask    string
	gateway    string
	dnsnames   string
	host       string
}

func (tpl *UbuntuCreateBridge) Options(t *templates.Template) {
	if bridgename, ok := t.Options[Bridgename]; ok {
		tpl.bridgename = bridgename
	}
	if phydev, ok := t.Options[PhyDev]; ok {
		tpl.phydev = phydev
	}
	if network, ok := t.Options[Network]; ok {
		tpl.network = network
	}
	if netmask, ok := t.Options[Netmask]; ok {
		tpl.netmask = netmask
	}
	if gateway, ok := t.Options[Gateway]; ok {
		tpl.gateway = gateway
	}
	if dnsnames, ok := t.Options[Dnsnames]; ok {
		tpl.dnsnames = dnsnames
	}
	if host, ok := t.Options[Host]; ok {
		tpl.host = host
	}
}

func (tpl *UbuntuCreateBridge) Render(p urknall.Package) {
	p.AddTemplate("bridge", &UbuntuCreateBridgeTemplate{
		bridgename: tpl.bridgename,
		phydev:     tpl.phydev,
		network:    tpl.network,
		netmask:    tpl.netmask,
		gateway:    tpl.gateway,
		dnsnames:   tpl.dnsnames,
		host:       tpl.host,
	})
}

func (tpl *UbuntuCreateBridge) Run(target urknall.Target, inputs []string) error {
	return urknall.Run(target, tpl, inputs)
}

type UbuntuCreateBridgeTemplate struct {
	bridgename string
	phydev     string
	network    string
	netmask    string
	gateway    string
	dnsnames   string
	host       string
}

func (m *UbuntuCreateBridgeTemplate) Render(pkg urknall.Package) {
	var dnsnames string
	ip := m.host
	bridgename := m.bridgename
	phydev := m.phydev
	network := m.network
	netmask := m.netmask
	gateway := m.gateway

	if m.phydev == "" {
		phydev = "eth0"
	}

	if m.bridgename == "" {
		bridgename = "one"
	}
	if m.dnsnames == "" {
		dnsnames = "8.8.8.8 8.8.4.4"
	} else {
		dnsnames = m.dnsnames
	}

	pkg.AddCommands("bridgeutils",
		Shell("apt-get install -y bridge-utils"),
	)
	pkg.AddCommands("interfaces",
		Shell("cp /etc/network/interfaces /etc/network/bkinterfaces"),
		WriteFile("/etc/network/interfaces", fmt.Sprintf(Interface, phydev, bridgename, bridgename, ip, network, netmask, gateway, phydev, dnsnames), "root", 0644),
	)
	pkg.AddCommands("create-bridge",
		Shell("brctl addbr "+bridgename+""),
	)
	pkg.AddCommands("list-bridge",
		Shell("brctl show"),
	)

}
<|endoftext|>


<commit_before>package controller

import "gosaic/environment"

func MacroList(env environment.Environment) {
	macroService, err := env.MacroService()
	if err != nil {
		env.Printf("Error getting macro service: %s\n", err.Error())
		return
	}

	macros, err := macroService.FindAll("macros.name ASC")
	if err != nil {
		env.Printf("Error finding macros: %s\n", err.Error())
		return
	}
	if len(macros) == 0 {
		// we are done
		return
	}

	for _, macro := range macros {
		env.Printf("%d %s (%dx%d)", macro.Id, macro.Path, macro.Width, macro.Height)
	}
}
<commit_msg>Add macro list detail<commit_after>package controller

import (
	"fmt"
	"gosaic/environment"
)

func MacroList(env environment.Environment) {
	macroService, err := env.MacroService()
	if err != nil {
		env.Printf("Error getting macro service: %s\n", err.Error())
		return
	}

	macroPartialService, err := env.MacroPartialService()
	if err != nil {
		env.Printf("Error getting macro partial service: %s\n", err.Error())
		return
	}

	coverService, err := env.CoverService()
	if err != nil {
		env.Printf("Error getting cover service: %s\n", err.Error())
		return
	}

	aspectService, err := env.AspectService()
	if err != nil {
		env.Printf("Error getting aspect service: %s\n", err.Error())
		return
	}

	macros, err := macroService.FindAll("macros.name ASC")
	if err != nil {
		env.Printf("Error finding macros: %s\n", err.Error())
		return
	}
	if len(macros) == 0 {
		// we are done
		return
	}

	for _, macro := range macros {
		fmt.Printf("ID: %d\n\tPath: %s (%dx%d)\n", macro.Id, macro.Path, macro.Width, macro.Height)

		aspectIds, err := macroPartialService.AspectIds(macro.Id)
		if err != nil {
			fmt.Printf("\tError getting aspect ids for macro partials: %s\n", err.Error())
		} else {
			aspects, err := aspectService.FindIn(aspectIds)
			if err != nil {
				fmt.Printf("\tError getting aspects for macro partials: %s\n", err.Error())
			} else {
				fmt.Printf("\tPartial aspects: ")
				for i, aspect := range aspects {
					fmt.Printf("%dx%d", aspect.Columns, aspect.Rows)
					if i < len(aspects)-1 {
						fmt.Printf(", ")
					}
				}
				fmt.Printf("\n")
			}
		}

		cover, err := coverService.Get(macro.CoverId)
		if err != nil {
			fmt.Printf("\tError getting cover details: %s\n", err.Error())
		} else {
			fmt.Printf("\tCover: %s\n", cover)
		}
	}
}
<|endoftext|>


<commit_before>/*
 * Copyright 2013 Nan Deng
 *
 * 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 msgcenter

import (
	"crypto/rsa"
	"fmt"
	"github.com/uniqush/uniqush-conn/config"
	"github.com/uniqush/uniqush-conn/proto/server"
	"github.com/uniqush/uniqush-conn/rpc"
	"net"
	"sync"
)

type MessageCenter struct {
	ln      net.Listener
	privkey *rsa.PrivateKey
	config  *config.Config

	srvCentersLock   sync.Mutex
	serviceCenterMap map[string]*serviceCenter
	fwdChan          chan *rpc.ForwardRequest
	peers            *rpc.MultiPeer
}

func (self *MessageCenter) processForwardRequest() {
	for req := range self.fwdChan {
		if req == nil {
			return
		}
		if len(req.ReceiverService) == 0 {
			continue
		}
		center := self.getServiceCenter(req.ReceiverService)
		if center != nil {
			go center.Forward(req)
		}
	}
}

func NewMessageCenter(ln net.Listener, privkey *rsa.PrivateKey, conf *config.Config) *MessageCenter {
	ret := new(MessageCenter)
	ret.ln = ln
	ret.privkey = privkey
	ret.config = conf

	ret.peers = rpc.NewMultiPeer()
	ret.fwdChan = make(chan *rpc.ForwardRequest)
	ret.serviceCenterMap = make(map[string]*serviceCenter, 10)

	go ret.processForwardRequest()
	return ret
}

func (self *MessageCenter) getServiceCenter(srv string) *serviceCenter {
	self.srvCentersLock.Lock()
	defer self.srvCentersLock.Unlock()

	center, ok := self.serviceCenterMap[srv]
	if !ok {
		conf := self.config.ReadConfig(srv)
		if conf == nil {
			return nil
		}
		center = newServiceCenter(conf, self.fwdChan, self.peers)
		if center != nil {
			self.serviceCenterMap[srv] = center
		}
	}
	return center
}

func (self *MessageCenter) serveConn(c net.Conn) {
	if tcpConn, ok := c.(*net.TCPConn); ok {
		tcpConn.SetKeepAlive(true)
	}
	conn, err := server.AuthConn(c, self.privkey, self.config, self.config.HandshakeTimeout)
	if err != nil {
		if err != server.ErrAuthFail {
			self.config.OnError(c.RemoteAddr(), err)
		}
		c.Close()
		return
	}
	srv := conn.Service()

	center := self.getServiceCenter(srv)
	if center == nil {
		self.config.OnError(c.RemoteAddr(), fmt.Errorf("unknown service: %v", srv))
		c.Close()
		return
	}
	center.NewConn(conn)
	return
}

func (self *MessageCenter) Start() {
	for {
		conn, err := self.ln.Accept()
		if err != nil {
			if ne, ok := err.(net.Error); ok {
				// It's a temporary error.
				if ne.Temporary() {
					continue
				}
			}
			self.config.OnError(self.ln.Addr(), err)
			return
		}
		go self.serveConn(conn)
	}
}

// NOTE: you cannot restart it!
func (self *MessageCenter) Stop() {
	self.srvCentersLock.Lock()

	for _, center := range self.serviceCenterMap {
		center.Stop()
	}
	self.ln.Close()
	close(self.fwdChan)
}

func (self *MessageCenter) do(srv string, f func(center *serviceCenter) *rpc.Result) *rpc.Result {
	center := self.getServiceCenter(srv)
	if center == nil {
		ret := new(rpc.Result)
		ret.SetError(fmt.Errorf("unknown service: %v", srv))
		return ret
	}
	return f(center)
}

func (self *MessageCenter) Send(req *rpc.SendRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Send(req)
	})
}

func (self *MessageCenter) Forward(req *rpc.ForwardRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Forward(req)
	})
}

func (self *MessageCenter) Redirect(req *rpc.RedirectRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Redirect(req)
	})
}

func (self *MessageCenter) CheckUserStatus(req *rpc.UserStatusQuery) *rpc.Result {
	return self.do(req.Service, func(center *serviceCenter) *rpc.Result {
		return center.CheckUserStatus(req)
	})
}

func (self *MessageCenter) AddPeer(peer rpc.UniqushConnPeer) {
	self.peers.AddPeer(peer)
}
<commit_msg>ServiceNames(). fixing #13<commit_after>/*
 * Copyright 2013 Nan Deng
 *
 * 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 msgcenter

import (
	"crypto/rsa"
	"fmt"
	"github.com/uniqush/uniqush-conn/config"
	"github.com/uniqush/uniqush-conn/proto/server"
	"github.com/uniqush/uniqush-conn/rpc"
	"net"
	"sync"
)

type MessageCenter struct {
	ln      net.Listener
	privkey *rsa.PrivateKey
	config  *config.Config

	srvCentersLock   sync.Mutex
	serviceCenterMap map[string]*serviceCenter
	fwdChan          chan *rpc.ForwardRequest
	peers            *rpc.MultiPeer
}

func (self *MessageCenter) processForwardRequest() {
	for req := range self.fwdChan {
		if req == nil {
			return
		}
		if len(req.ReceiverService) == 0 {
			continue
		}
		center := self.getServiceCenter(req.ReceiverService)
		if center != nil {
			go center.Forward(req)
		}
	}
}

func NewMessageCenter(ln net.Listener, privkey *rsa.PrivateKey, conf *config.Config) *MessageCenter {
	ret := new(MessageCenter)
	ret.ln = ln
	ret.privkey = privkey
	ret.config = conf

	ret.peers = rpc.NewMultiPeer()
	ret.fwdChan = make(chan *rpc.ForwardRequest)
	ret.serviceCenterMap = make(map[string]*serviceCenter, 10)

	go ret.processForwardRequest()
	return ret
}

func (self *MessageCenter) ServiceNames() []string {
	self.srvCentersLock.Lock()
	defer self.srvCentersLock.Unlock()

	ret := make([]string, 0, len(self.serviceCenterMap))

	for srv, _ := range self.serviceCenterMap {
		ret = append(ret, srv)
	}
	return ret
}

func (self *MessageCenter) getServiceCenter(srv string) *serviceCenter {
	self.srvCentersLock.Lock()
	defer self.srvCentersLock.Unlock()

	center, ok := self.serviceCenterMap[srv]
	if !ok {
		conf := self.config.ReadConfig(srv)
		if conf == nil {
			return nil
		}
		center = newServiceCenter(conf, self.fwdChan, self.peers)
		if center != nil {
			self.serviceCenterMap[srv] = center
		}
	}
	return center
}

func (self *MessageCenter) serveConn(c net.Conn) {
	if tcpConn, ok := c.(*net.TCPConn); ok {
		tcpConn.SetKeepAlive(true)
	}
	conn, err := server.AuthConn(c, self.privkey, self.config, self.config.HandshakeTimeout)
	if err != nil {
		if err != server.ErrAuthFail {
			self.config.OnError(c.RemoteAddr(), err)
		}
		c.Close()
		return
	}
	srv := conn.Service()

	center := self.getServiceCenter(srv)
	if center == nil {
		self.config.OnError(c.RemoteAddr(), fmt.Errorf("unknown service: %v", srv))
		c.Close()
		return
	}
	center.NewConn(conn)
	return
}

func (self *MessageCenter) Start() {
	for {
		conn, err := self.ln.Accept()
		if err != nil {
			if ne, ok := err.(net.Error); ok {
				// It's a temporary error.
				if ne.Temporary() {
					continue
				}
			}
			self.config.OnError(self.ln.Addr(), err)
			return
		}
		go self.serveConn(conn)
	}
}

// NOTE: you cannot restart it!
func (self *MessageCenter) Stop() {
	self.srvCentersLock.Lock()

	for _, center := range self.serviceCenterMap {
		center.Stop()
	}
	self.ln.Close()
	close(self.fwdChan)
}

func (self *MessageCenter) do(srv string, f func(center *serviceCenter) *rpc.Result) *rpc.Result {
	center := self.getServiceCenter(srv)
	if center == nil {
		ret := new(rpc.Result)
		ret.SetError(fmt.Errorf("unknown service: %v", srv))
		return ret
	}
	return f(center)
}

func (self *MessageCenter) Send(req *rpc.SendRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Send(req)
	})
}

func (self *MessageCenter) Forward(req *rpc.ForwardRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Forward(req)
	})
}

func (self *MessageCenter) Redirect(req *rpc.RedirectRequest) *rpc.Result {
	return self.do(req.ReceiverService, func(center *serviceCenter) *rpc.Result {
		return center.Redirect(req)
	})
}

func (self *MessageCenter) CheckUserStatus(req *rpc.UserStatusQuery) *rpc.Result {
	return self.do(req.Service, func(center *serviceCenter) *rpc.Result {
		return center.CheckUserStatus(req)
	})
}

func (self *MessageCenter) AddPeer(peer rpc.UniqushConnPeer) {
	self.peers.AddPeer(peer)
}
<|endoftext|>


<commit_before>// Copyright 2020 Google LLC
//
// 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
//
//     https://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.
//
// The trace command is an example of setting up OpenTelemetry to export traces to Google
// Cloud Trace.
package main

// [START opentelemetry_trace_import]
import (
	"context"
	"log"
	"os"

	texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// [END opentelemetry_trace_import]
// [START opentelemetry_trace_main_function]
func main() {
	// Create exporter.
	ctx := context.Background()
	projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
	exporter, err := texporter.NewExporter(texporter.WithProjectID(projectID))
	if err != nil {
		log.Fatalf("texporter.NewExporter: %v", err)
	}
	defer exporter.Shutdown(ctx) // flushes any pending spans

	// Create trace provider with the exporter.
	//
	// By default it uses AlwaysSample() which samples all traces.
	// In a production environment or high QPS setup please use
	// probabilistic sampling.
	// Example:
	//   tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.TraceIDRatioBased(0.0001)), ...)
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	otel.SetTracerProvider(tp)

	// [START opentelemetry_trace_custom_span]
	// Create custom span.
	tracer := otel.GetTracerProvider().Tracer("example.com/trace")
	err = func(ctx context.Context) error {
		ctx, span := tracer.Start(ctx, "foo")
		defer span.End()

		// Do some work.

		return nil
	}(ctx)
	// [END opentelemetry_trace_custom_span]
}

// [END opentelemetry_trace_main_function]
<commit_msg>fix(opentelemetry): use the right flush method (#2095)<commit_after>// Copyright 2020 Google LLC
//
// 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
//
//     https://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.
//
// The trace command is an example of setting up OpenTelemetry to export traces to Google
// Cloud Trace.
package main

// [START opentelemetry_trace_import]
import (
	"context"
	"log"
	"os"

	texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// [END opentelemetry_trace_import]
// [START opentelemetry_trace_main_function]
func main() {
	// Create exporter.
	ctx := context.Background()
	projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
	exporter, err := texporter.NewExporter(texporter.WithProjectID(projectID))
	if err != nil {
		log.Fatalf("texporter.NewExporter: %v", err)
	}

	// Create trace provider with the exporter.
	//
	// By default it uses AlwaysSample() which samples all traces.
	// In a production environment or high QPS setup please use
	// probabilistic sampling.
	// Example:
	//   tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.TraceIDRatioBased(0.0001)), ...)
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	defer tp.ForceFlush(ctx) // flushes any pending spans
	otel.SetTracerProvider(tp)

	// [START opentelemetry_trace_custom_span]
	// Create custom span.
	tracer := otel.GetTracerProvider().Tracer("example.com/trace")
	err = func(ctx context.Context) error {
		ctx, span := tracer.Start(ctx, "foo")
		defer span.End()

		// Do some work.

		return nil
	}(ctx)
	// [END opentelemetry_trace_custom_span]
}

// [END opentelemetry_trace_main_function]
<|endoftext|>


<commit_before>// Take well-formed json from either stdin or an input file and create an elasticsearch document to be used to
// generate user specific dashboards or highly contextual alerts.
//
// LICENSE:
//   Copyright 2015 Yieldbot. <devops@yieldbot.com>
//   Released under the MIT License; see LICENSE
//   for details.

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/olivere/elastic"
	"github.com/yieldbot/dracky/src"
	"io/ioutil"
	"os"
	"time"
)

func main() {

	// set commandline flags
	esIndexPtr := flag.String("index", dracky.StatusEsIndex, "the elasticsearch index to use")
	esHostPtr := flag.String("host", dracky.DefaultEsHost, "the elasticsearch host")
	esPortPtr := flag.String("port", dracky.DefaultEsPort, "the elasticsearch port")
	stdinPtr := flag.Bool("read-stdin", true, "read input from stdin")
	//timePtr := flag.string("t-format", "", "time format to suffix on the index name")
	inputFilePtr := flag.String("input-file", "", "file to read json in from, check docs for proper format")

	flag.Parse()
	esIndex := *esIndexPtr
	esType := dracky.DefaultEsType
	esHost := *esHostPtr
	esPort := *esPortPtr
	rdStdin := *stdinPtr
	inputFile := *inputFilePtr

	// I don't want to call these if they are not needed
	sensuEvent := new(dracky.SensuEvent)
	userEvent := new(dracky.UserEvent)
	//t_format := *timePtr

	sensuEnv := dracky.SetSensuEnv()

	//   if t_format != "" {
	//     // get the format of the time
	//     esIndex = esIndex + t_format
	//   }

	if (rdStdin == false) && (inputFile != "") {
		userInput, err := ioutil.ReadFile(inputFile)
		if err != nil {
			dracky.Check(err)
		}
		err = json.Unmarshal(userInput, &userEvent)
		if err != nil {
			dracky.Check(err)
		}
		esType = "user"
	} else if (rdStdin == false) && (inputFile == "") {
		fmt.Printf("Please enter a file to read from")
		os.Exit(1)
	} else {
		sensuEvent = sensuEvent.AcquireSensuEvent()
	}

	// Create a client
	client, err := elastic.NewClient(
		elastic.SetURL("http://" + esHost + ":" + esPort),
	)
	if err != nil {
		dracky.Check(err)
	}

	// Check to see if the index exists and if not create it
	if client.IndexExists(esIndex) == nil { // need to test to make sure this does what I want
  	_, err = client.CreateIndex(esIndex).Do()
  	if err != nil {
  		dracky.Check(err)
  	}
	}

	// Create an Elasticsearch document. The document type will define the mapping used for the document.
	doc := make(map[string]string)
	var docID string
	switch esType {
	case "sensu":
		docID = dracky.EventName(sensuEvent.Client.Name, sensuEvent.Check.Name)
		doc["monitored_instance"] = sensuEvent.AcquireMonitoredInstance()
		doc["sensu_client"] = sensuEvent.Client.Name
		doc["incident_timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339)
		doc["check_name"] = dracky.CreateCheckName(sensuEvent.Check.Name)
		doc["check_state"] = dracky.DefineStatus(sensuEvent.Check.Status)
		doc["sensuEnv"] = dracky.DefineSensuEnv(sensuEnv.Sensu.Environment)
		doc["instance_address"] = sensuEvent.Client.Address
		doc["check_state_duration"] = dracky.DefineCheckStateDuration()
	case "user":
		doc["product"] = userEvent.Product
		doc["data"] = userEvent.Data
		doc["timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339) // dracky.Set_time(userEvent.Timestamp)
	default:
		fmt.Printf("Type is not correctly set")
		os.Exit(2)
	}

	// Add a document to the Elasticsearch index
	_, err = client.Index().
		Index(esIndex).
		Type(esType).
		Id(docID).
		BodyJson(doc).
		Do()
	if err != nil {
		dracky.Check(err)
	}

	// Log a successful document push to stdout. I don't add the id here as some id's are fixed but
	// the user has the ability to autogenerate an id if they don't want to provide one.
	fmt.Printf("Record added to ES\n")
}
<commit_msg>fix formatting issues<commit_after>// Take well-formed json from either stdin or an input file and create an elasticsearch document to be used to
// generate user specific dashboards or highly contextual alerts.
//
// LICENSE:
//   Copyright 2015 Yieldbot. <devops@yieldbot.com>
//   Released under the MIT License; see LICENSE
//   for details.

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/olivere/elastic"
	"github.com/yieldbot/dracky/src"
	"io/ioutil"
	"os"
	"time"
)

func main() {

	// set commandline flags
	esIndexPtr := flag.String("index", dracky.StatusEsIndex, "the elasticsearch index to use")
	esHostPtr := flag.String("host", dracky.DefaultEsHost, "the elasticsearch host")
	esPortPtr := flag.String("port", dracky.DefaultEsPort, "the elasticsearch port")
	stdinPtr := flag.Bool("read-stdin", true, "read input from stdin")
	//timePtr := flag.string("t-format", "", "time format to suffix on the index name")
	inputFilePtr := flag.String("input-file", "", "file to read json in from, check docs for proper format")

	flag.Parse()
	esIndex := *esIndexPtr
	esType := dracky.DefaultEsType
	esHost := *esHostPtr
	esPort := *esPortPtr
	rdStdin := *stdinPtr
	inputFile := *inputFilePtr

	// I don't want to call these if they are not needed
	sensuEvent := new(dracky.SensuEvent)
	userEvent := new(dracky.UserEvent)
	//t_format := *timePtr

	sensuEnv := dracky.SetSensuEnv()

	//   if t_format != "" {
	//     // get the format of the time
	//     esIndex = esIndex + t_format
	//   }

	if (rdStdin == false) && (inputFile != "") {
		userInput, err := ioutil.ReadFile(inputFile)
		if err != nil {
			dracky.Check(err)
		}
		err = json.Unmarshal(userInput, &userEvent)
		if err != nil {
			dracky.Check(err)
		}
		esType = "user"
	} else if (rdStdin == false) && (inputFile == "") {
		fmt.Printf("Please enter a file to read from")
		os.Exit(1)
	} else {
		sensuEvent = sensuEvent.AcquireSensuEvent()
	}

	// Create a client
	client, err := elastic.NewClient(
		elastic.SetURL("http://" + esHost + ":" + esPort),
	)
	if err != nil {
		dracky.Check(err)
	}

	// Check to see if the index exists and if not create it
	if client.IndexExists(esIndex) == nil { // need to test to make sure this does what I want
		_, err = client.CreateIndex(esIndex).Do()
		if err != nil {
			dracky.Check(err)
		}
	}

	// Create an Elasticsearch document. The document type will define the mapping used for the document.
	doc := make(map[string]string)
	var docID string
	switch esType {
	case "sensu":
		docID = dracky.EventName(sensuEvent.Client.Name, sensuEvent.Check.Name)
		doc["monitored_instance"] = sensuEvent.AcquireMonitoredInstance()
		doc["sensu_client"] = sensuEvent.Client.Name
		doc["incident_timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339)
		doc["check_name"] = dracky.CreateCheckName(sensuEvent.Check.Name)
		doc["check_state"] = dracky.DefineStatus(sensuEvent.Check.Status)
		doc["sensuEnv"] = dracky.DefineSensuEnv(sensuEnv.Sensu.Environment)
		doc["instance_address"] = sensuEvent.Client.Address
		doc["check_state_duration"] = dracky.DefineCheckStateDuration()
	case "user":
		doc["product"] = userEvent.Product
		doc["data"] = userEvent.Data
		doc["timestamp"] = time.Unix(sensuEvent.Check.Issued, 0).Format(time.RFC3339) // dracky.Set_time(userEvent.Timestamp)
	default:
		fmt.Printf("Type is not correctly set")
		os.Exit(2)
	}

	// Add a document to the Elasticsearch index
	_, err = client.Index().
		Index(esIndex).
		Type(esType).
		Id(docID).
		BodyJson(doc).
		Do()
	if err != nil {
		dracky.Check(err)
	}

	// Log a successful document push to stdout. I don't add the id here as some id's are fixed but
	// the user has the ability to autogenerate an id if they don't want to provide one.
	fmt.Printf("Record added to ES\n")
}
<|endoftext|>


<commit_before>// Copyright 2015 Matthew Holt and The Caddy Authors
//
// 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 fileserver

import (
	"bytes"
	"fmt"
	"html/template"
	weakrand "math/rand"
	"mime"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/caddyserver/caddy/v2"
	"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)

func init() {
	weakrand.Seed(time.Now().UnixNano())

	caddy.RegisterModule(FileServer{})
}

// FileServer implements a static file server responder for Caddy.
type FileServer struct {
	Root       string   `json:"root,omitempty"` // default is current directory
	Hide       []string `json:"hide,omitempty"`
	IndexNames []string `json:"index_names,omitempty"`
	Browse     *Browse  `json:"browse,omitempty"`
}

// CaddyModule returns the Caddy module information.
func (FileServer) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		Name: "http.handlers.file_server",
		New:  func() caddy.Module { return new(FileServer) },
	}
}

// Provision sets up the static files responder.
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
	if fsrv.IndexNames == nil {
		fsrv.IndexNames = defaultIndexNames
	}

	if fsrv.Browse != nil {
		var tpl *template.Template
		var err error
		if fsrv.Browse.TemplateFile != "" {
			tpl, err = template.ParseFiles(fsrv.Browse.TemplateFile)
			if err != nil {
				return fmt.Errorf("parsing browse template file: %v", err)
			}
		} else {
			tpl, err = template.New("default_listing").Parse(defaultBrowseTemplate)
			if err != nil {
				return fmt.Errorf("parsing default browse template: %v", err)
			}
		}
		fsrv.Browse.template = tpl
	}

	return nil
}

func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
	repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)

	filesToHide := fsrv.transformHidePaths(repl)

	root := repl.ReplaceAll(fsrv.Root, ".")
	suffix := repl.ReplaceAll(r.URL.Path, "")
	filename := sanitizedPathJoin(root, suffix)

	// get information about the file
	info, err := os.Stat(filename)
	if err != nil {
		err = mapDirOpenError(err, filename)
		if os.IsNotExist(err) {
			return caddyhttp.Error(http.StatusNotFound, err)
		} else if os.IsPermission(err) {
			return caddyhttp.Error(http.StatusForbidden, err)
		}
		// TODO: treat this as resource exhaustion like with os.Open? Or unnecessary here?
		return caddyhttp.Error(http.StatusInternalServerError, err)
	}

	// if the request mapped to a directory, see if
	// there is an index file we can serve
	if info.IsDir() && len(fsrv.IndexNames) > 0 {
		for _, indexPage := range fsrv.IndexNames {
			indexPath := sanitizedPathJoin(filename, indexPage)
			if fileHidden(indexPath, filesToHide) {
				// pretend this file doesn't exist
				continue
			}

			indexInfo, err := os.Stat(indexPath)
			if err != nil {
				continue
			}

			// we found an index file that might work,
			// so rewrite the request path
			r.URL.Path = path.Join(r.URL.Path, indexPage)

			info = indexInfo
			filename = indexPath
			break
		}
	}

	// if still referencing a directory, delegate
	// to browse or return an error
	if info.IsDir() {
		if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
			return fsrv.serveBrowse(filename, w, r)
		}
		return caddyhttp.Error(http.StatusNotFound, nil)
	}

	// TODO: content negotiation (brotli sidecar files, etc...)

	// one last check to ensure the file isn't hidden (we might
	// have changed the filename from when we last checked)
	if fileHidden(filename, filesToHide) {
		return caddyhttp.Error(http.StatusNotFound, nil)
	}

	// open the file
	file, err := fsrv.openFile(filename, w)
	if err != nil {
		return err
	}
	defer file.Close()

	// set the ETag - note that a conditional If-None-Match request is handled
	// by http.ServeContent below, which checks against this ETag value
	w.Header().Set("ETag", calculateEtag(info))

	if w.Header().Get("Content-Type") == "" {
		mtyp := mime.TypeByExtension(filepath.Ext(filename))
		if mtyp == "" {
			// do not allow Go to sniff the content-type; see
			// https://www.youtube.com/watch?v=8t8JYpt0egE
			// TODO: Consider writing a default mime type of application/octet-stream - this is secure but violates spec
			w.Header()["Content-Type"] = nil
		} else {
			w.Header().Set("Content-Type", mtyp)
		}
	}

	// let the standard library do what it does best; note, however,
	// that errors generated by ServeContent are written immediately
	// to the response, so we cannot handle them (but errors there
	// are rare)
	http.ServeContent(w, r, info.Name(), info.ModTime(), file)

	return nil
}

// openFile opens the file at the given filename. If there was an error,
// the response is configured to inform the client how to best handle it
// and a well-described handler error is returned (do not wrap the
// returned error value).
func (fsrv *FileServer) openFile(filename string, w http.ResponseWriter) (*os.File, error) {
	file, err := os.Open(filename)
	if err != nil {
		err = mapDirOpenError(err, filename)
		if os.IsNotExist(err) {
			return nil, caddyhttp.Error(http.StatusNotFound, err)
		} else if os.IsPermission(err) {
			return nil, caddyhttp.Error(http.StatusForbidden, err)
		}
		// maybe the server is under load and ran out of file descriptors?
		// have client wait arbitrary seconds to help prevent a stampede
		backoff := weakrand.Intn(maxBackoff-minBackoff) + minBackoff
		w.Header().Set("Retry-After", strconv.Itoa(backoff))
		return nil, caddyhttp.Error(http.StatusServiceUnavailable, err)
	}
	return file, nil
}

// mapDirOpenError maps the provided non-nil error from opening name
// to a possibly better non-nil error. In particular, it turns OS-specific errors
// about opening files in non-directories into os.ErrNotExist. See golang/go#18984.
// Adapted from the Go standard library; originally written by Nathaniel Caza.
// https://go-review.googlesource.com/c/go/+/36635/
// https://go-review.googlesource.com/c/go/+/36804/
func mapDirOpenError(originalErr error, name string) error {
	if os.IsNotExist(originalErr) || os.IsPermission(originalErr) {
		return originalErr
	}

	parts := strings.Split(name, string(filepath.Separator))
	for i := range parts {
		if parts[i] == "" {
			continue
		}
		fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
		if err != nil {
			return originalErr
		}
		if !fi.IsDir() {
			return os.ErrNotExist
		}
	}

	return originalErr
}

// transformHidePaths performs replacements for all the elements of
// fsrv.Hide and returns a new list of the transformed values.
func (fsrv *FileServer) transformHidePaths(repl caddy.Replacer) []string {
	hide := make([]string, len(fsrv.Hide))
	for i := range fsrv.Hide {
		hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
	}
	return hide
}

// sanitizedPathJoin performs filepath.Join(root, reqPath) that
// is safe against directory traversal attacks. It uses logic
// similar to that in the Go standard library, specifically
// in the implementation of http.Dir. The root is assumed to
// be a trusted path, but reqPath is not.
func sanitizedPathJoin(root, reqPath string) string {
	// TODO: Caddy 1 uses this:
	// prevent absolute path access on Windows, e.g. http://localhost:5000/C:\Windows\notepad.exe
	// if runtime.GOOS == "windows" && len(reqPath) > 0 && filepath.IsAbs(reqPath[1:]) {
	// TODO.
	// }

	// TODO: whereas std lib's http.Dir.Open() uses this:
	// if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
	// 	return nil, errors.New("http: invalid character in file path")
	// }

	// TODO: see https://play.golang.org/p/oh77BiVQFti for another thing to consider

	if root == "" {
		root = "."
	}
	return filepath.Join(root, filepath.FromSlash(path.Clean("/"+reqPath)))
}

// fileHidden returns true if filename is hidden
// according to the hide list.
func fileHidden(filename string, hide []string) bool {
	nameOnly := filepath.Base(filename)
	sep := string(filepath.Separator)

	for _, h := range hide {
		// assuming h is a glob/shell-like pattern,
		// use it to compare the whole file path;
		// but if there is no separator in h, then
		// just compare against the file's name
		compare := filename
		if !strings.Contains(h, sep) {
			compare = nameOnly
		}

		hidden, err := filepath.Match(h, compare)
		if err != nil {
			// malformed pattern; fallback by checking prefix
			if strings.HasPrefix(filename, h) {
				return true
			}
		}
		if hidden {
			// file name or path matches hide pattern
			return true
		}
	}

	return false
}

// calculateEtag produces a strong etag by default, although, for
// efficiency reasons, it does not actually consume the contents
// of the file to make a hash of all the bytes. ¯\_(ツ)_/¯
// Prefix the etag with "W/" to convert it into a weak etag.
// See: https://tools.ietf.org/html/rfc7232#section-2.3
func calculateEtag(d os.FileInfo) string {
	t := strconv.FormatInt(d.ModTime().Unix(), 36)
	s := strconv.FormatInt(d.Size(), 36)
	return `"` + t + s + `"`
}

var defaultIndexNames = []string{"index.html", "index.txt"}

var bufPool = sync.Pool{
	New: func() interface{} {
		return new(bytes.Buffer)
	},
}

const minBackoff, maxBackoff = 2, 5

// Interface guards
var (
	_ caddy.Provisioner           = (*FileServer)(nil)
	_ caddyhttp.MiddlewareHandler = (*FileServer)(nil)
)
<commit_msg>file_server: Enforce URL canonicalization (closes #2741)<commit_after>// Copyright 2015 Matthew Holt and The Caddy Authors
//
// 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 fileserver

import (
	"bytes"
	"fmt"
	"html/template"
	weakrand "math/rand"
	"mime"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/caddyserver/caddy/v2"
	"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)

func init() {
	weakrand.Seed(time.Now().UnixNano())

	caddy.RegisterModule(FileServer{})
}

// FileServer implements a static file server responder for Caddy.
type FileServer struct {
	Root          string   `json:"root,omitempty"` // default is current directory
	Hide          []string `json:"hide,omitempty"`
	IndexNames    []string `json:"index_names,omitempty"`
	Browse        *Browse  `json:"browse,omitempty"`
	CanonicalURIs *bool    `json:"canonical_uris,omitempty"`
}

// CaddyModule returns the Caddy module information.
func (FileServer) CaddyModule() caddy.ModuleInfo {
	return caddy.ModuleInfo{
		Name: "http.handlers.file_server",
		New:  func() caddy.Module { return new(FileServer) },
	}
}

// Provision sets up the static files responder.
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
	if fsrv.Root == "" {
		fsrv.Root = "{http.vars.root}"
	}

	if fsrv.IndexNames == nil {
		fsrv.IndexNames = defaultIndexNames
	}

	if fsrv.Browse != nil {
		var tpl *template.Template
		var err error
		if fsrv.Browse.TemplateFile != "" {
			tpl, err = template.ParseFiles(fsrv.Browse.TemplateFile)
			if err != nil {
				return fmt.Errorf("parsing browse template file: %v", err)
			}
		} else {
			tpl, err = template.New("default_listing").Parse(defaultBrowseTemplate)
			if err != nil {
				return fmt.Errorf("parsing default browse template: %v", err)
			}
		}
		fsrv.Browse.template = tpl
	}

	return nil
}

func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
	repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)

	filesToHide := fsrv.transformHidePaths(repl)

	root := repl.ReplaceAll(fsrv.Root, ".")
	suffix := repl.ReplaceAll(r.URL.Path, "")
	filename := sanitizedPathJoin(root, suffix)

	// get information about the file
	info, err := os.Stat(filename)
	if err != nil {
		err = mapDirOpenError(err, filename)
		if os.IsNotExist(err) {
			return caddyhttp.Error(http.StatusNotFound, err)
		} else if os.IsPermission(err) {
			return caddyhttp.Error(http.StatusForbidden, err)
		}
		// TODO: treat this as resource exhaustion like with os.Open? Or unnecessary here?
		return caddyhttp.Error(http.StatusInternalServerError, err)
	}

	// if the request mapped to a directory, see if
	// there is an index file we can serve
	var implicitIndexFile bool
	if info.IsDir() && len(fsrv.IndexNames) > 0 {
		for _, indexPage := range fsrv.IndexNames {
			indexPath := sanitizedPathJoin(filename, indexPage)
			if fileHidden(indexPath, filesToHide) {
				// pretend this file doesn't exist
				continue
			}

			indexInfo, err := os.Stat(indexPath)
			if err != nil {
				continue
			}

			// don't rewrite the request path to append
			// the index file, because we might need to
			// do a canonical-URL redirect below based
			// on the URL as-is

			// we've chosen to use this index file,
			// so replace the last file info and path
			// with that of the index file
			info = indexInfo
			filename = indexPath
			implicitIndexFile = true
			break
		}
	}

	// if still referencing a directory, delegate
	// to browse or return an error
	if info.IsDir() {
		if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
			return fsrv.serveBrowse(filename, w, r)
		}
		return caddyhttp.Error(http.StatusNotFound, nil)
	}

	// TODO: content negotiation (brotli sidecar files, etc...)

	// one last check to ensure the file isn't hidden (we might
	// have changed the filename from when we last checked)
	if fileHidden(filename, filesToHide) {
		return caddyhttp.Error(http.StatusNotFound, nil)
	}

	// if URL canonicalization is enabled, we need to enforce trailing
	// slash convention: if a directory, trailing slash; if a file, no
	// trailing slash - not enforcing this can break relative hrefs
	// in HTML (see https://github.com/caddyserver/caddy/issues/2741)
	if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
		if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
			return redirect(w, r, r.URL.Path+"/")
		} else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
			return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
		}
	}

	// open the file
	file, err := fsrv.openFile(filename, w)
	if err != nil {
		return err // error is already structured
	}
	defer file.Close()

	// set the ETag - note that a conditional If-None-Match request is handled
	// by http.ServeContent below, which checks against this ETag value
	w.Header().Set("ETag", calculateEtag(info))

	if w.Header().Get("Content-Type") == "" {
		mtyp := mime.TypeByExtension(filepath.Ext(filename))
		if mtyp == "" {
			// do not allow Go to sniff the content-type; see
			// https://www.youtube.com/watch?v=8t8JYpt0egE
			// TODO: Consider writing a default mime type of application/octet-stream - this is secure but violates spec
			w.Header()["Content-Type"] = nil
		} else {
			w.Header().Set("Content-Type", mtyp)
		}
	}

	// let the standard library do what it does best; note, however,
	// that errors generated by ServeContent are written immediately
	// to the response, so we cannot handle them (but errors there
	// are rare)
	http.ServeContent(w, r, info.Name(), info.ModTime(), file)

	return nil
}

// openFile opens the file at the given filename. If there was an error,
// the response is configured to inform the client how to best handle it
// and a well-described handler error is returned (do not wrap the
// returned error value).
func (fsrv *FileServer) openFile(filename string, w http.ResponseWriter) (*os.File, error) {
	file, err := os.Open(filename)
	if err != nil {
		err = mapDirOpenError(err, filename)
		if os.IsNotExist(err) {
			return nil, caddyhttp.Error(http.StatusNotFound, err)
		} else if os.IsPermission(err) {
			return nil, caddyhttp.Error(http.StatusForbidden, err)
		}
		// maybe the server is under load and ran out of file descriptors?
		// have client wait arbitrary seconds to help prevent a stampede
		backoff := weakrand.Intn(maxBackoff-minBackoff) + minBackoff
		w.Header().Set("Retry-After", strconv.Itoa(backoff))
		return nil, caddyhttp.Error(http.StatusServiceUnavailable, err)
	}
	return file, nil
}

// mapDirOpenError maps the provided non-nil error from opening name
// to a possibly better non-nil error. In particular, it turns OS-specific errors
// about opening files in non-directories into os.ErrNotExist. See golang/go#18984.
// Adapted from the Go standard library; originally written by Nathaniel Caza.
// https://go-review.googlesource.com/c/go/+/36635/
// https://go-review.googlesource.com/c/go/+/36804/
func mapDirOpenError(originalErr error, name string) error {
	if os.IsNotExist(originalErr) || os.IsPermission(originalErr) {
		return originalErr
	}

	parts := strings.Split(name, string(filepath.Separator))
	for i := range parts {
		if parts[i] == "" {
			continue
		}
		fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
		if err != nil {
			return originalErr
		}
		if !fi.IsDir() {
			return os.ErrNotExist
		}
	}

	return originalErr
}

// transformHidePaths performs replacements for all the elements of
// fsrv.Hide and returns a new list of the transformed values.
func (fsrv *FileServer) transformHidePaths(repl caddy.Replacer) []string {
	hide := make([]string, len(fsrv.Hide))
	for i := range fsrv.Hide {
		hide[i] = repl.ReplaceAll(fsrv.Hide[i], "")
	}
	return hide
}

// sanitizedPathJoin performs filepath.Join(root, reqPath) that
// is safe against directory traversal attacks. It uses logic
// similar to that in the Go standard library, specifically
// in the implementation of http.Dir. The root is assumed to
// be a trusted path, but reqPath is not.
func sanitizedPathJoin(root, reqPath string) string {
	// TODO: Caddy 1 uses this:
	// prevent absolute path access on Windows, e.g. http://localhost:5000/C:\Windows\notepad.exe
	// if runtime.GOOS == "windows" && len(reqPath) > 0 && filepath.IsAbs(reqPath[1:]) {
	// TODO.
	// }

	// TODO: whereas std lib's http.Dir.Open() uses this:
	// if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
	// 	return nil, errors.New("http: invalid character in file path")
	// }

	// TODO: see https://play.golang.org/p/oh77BiVQFti for another thing to consider

	if root == "" {
		root = "."
	}
	return filepath.Join(root, filepath.FromSlash(path.Clean("/"+reqPath)))
}

// fileHidden returns true if filename is hidden
// according to the hide list.
func fileHidden(filename string, hide []string) bool {
	nameOnly := filepath.Base(filename)
	sep := string(filepath.Separator)

	for _, h := range hide {
		// assuming h is a glob/shell-like pattern,
		// use it to compare the whole file path;
		// but if there is no separator in h, then
		// just compare against the file's name
		compare := filename
		if !strings.Contains(h, sep) {
			compare = nameOnly
		}

		hidden, err := filepath.Match(h, compare)
		if err != nil {
			// malformed pattern; fallback by checking prefix
			if strings.HasPrefix(filename, h) {
				return true
			}
		}
		if hidden {
			// file name or path matches hide pattern
			return true
		}
	}

	return false
}

// calculateEtag produces a strong etag by default, although, for
// efficiency reasons, it does not actually consume the contents
// of the file to make a hash of all the bytes. ¯\_(ツ)_/¯
// Prefix the etag with "W/" to convert it into a weak etag.
// See: https://tools.ietf.org/html/rfc7232#section-2.3
func calculateEtag(d os.FileInfo) string {
	t := strconv.FormatInt(d.ModTime().Unix(), 36)
	s := strconv.FormatInt(d.Size(), 36)
	return `"` + t + s + `"`
}

func redirect(w http.ResponseWriter, r *http.Request, to string) error {
	for strings.HasPrefix(to, "//") {
		// prevent path-based open redirects
		to = strings.TrimPrefix(to, "/")
	}
	http.Redirect(w, r, to, http.StatusPermanentRedirect)
	return nil
}

var defaultIndexNames = []string{"index.html", "index.txt"}

var bufPool = sync.Pool{
	New: func() interface{} {
		return new(bytes.Buffer)
	},
}

const minBackoff, maxBackoff = 2, 5

// Interface guards
var (
	_ caddy.Provisioner           = (*FileServer)(nil)
	_ caddyhttp.MiddlewareHandler = (*FileServer)(nil)
)
<|endoftext|>


<commit_before>package backrest

/*
 Copyright 2019 Crunchy Data Solutions, Inc.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

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

	crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
	"github.com/crunchydata/postgres-operator/config"
	"github.com/crunchydata/postgres-operator/events"
	"github.com/crunchydata/postgres-operator/kubeapi"
	"github.com/crunchydata/postgres-operator/operator"
	log "github.com/sirupsen/logrus"
	v1batch "k8s.io/api/batch/v1"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

type backrestJobTemplateFields struct {
	JobName                       string
	Name                          string
	ClusterName                   string
	Command                       string
	CommandOpts                   string
	PITRTarget                    string
	PodName                       string
	PGOImagePrefix                string
	PGOImageTag                   string
	SecurityContext               string
	PgbackrestStanza              string
	PgbackrestDBPath              string
	PgbackrestRepoPath            string
	PgbackrestRepoType            string
	BackrestLocalAndS3Storage     bool
	PgbackrestRestoreVolumes      string
	PgbackrestRestoreVolumeMounts string
}

// Backrest ...
func Backrest(namespace string, clientset *kubernetes.Clientset, task *crv1.Pgtask) {

	//create the Job to run the backrest command

	cmd := task.Spec.Parameters[config.LABEL_BACKREST_COMMAND]

	jobFields := backrestJobTemplateFields{
		JobName:                       task.Spec.Parameters[config.LABEL_JOB_NAME],
		ClusterName:                   task.Spec.Parameters[config.LABEL_PG_CLUSTER],
		PodName:                       task.Spec.Parameters[config.LABEL_POD_NAME],
		SecurityContext:               "",
		Command:                       cmd,
		CommandOpts:                   task.Spec.Parameters[config.LABEL_BACKREST_OPTS],
		PITRTarget:                    "",
		PGOImagePrefix:                operator.Pgo.Pgo.PGOImagePrefix,
		PGOImageTag:                   operator.Pgo.Pgo.PGOImageTag,
		PgbackrestStanza:              task.Spec.Parameters[config.LABEL_PGBACKREST_STANZA],
		PgbackrestDBPath:              task.Spec.Parameters[config.LABEL_PGBACKREST_DB_PATH],
		PgbackrestRepoPath:            task.Spec.Parameters[config.LABEL_PGBACKREST_REPO_PATH],
		PgbackrestRestoreVolumes:      "",
		PgbackrestRestoreVolumeMounts: "",
		PgbackrestRepoType:            operator.GetRepoType(task.Spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE]),
		BackrestLocalAndS3Storage:     operator.IsLocalAndS3Storage(task.Spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE]),
	}

	var doc2 bytes.Buffer
	err := config.BackrestjobTemplate.Execute(&doc2, jobFields)
	if err != nil {
		log.Error(err.Error())
		return
	}

	if operator.CRUNCHY_DEBUG {
		config.BackrestjobTemplate.Execute(os.Stdout, jobFields)
	}

	newjob := v1batch.Job{}
	err = json.Unmarshal(doc2.Bytes(), &newjob)
	if err != nil {
		log.Error("error unmarshalling json into Job " + err.Error())
		return
	}

	newjob.ObjectMeta.Labels[config.LABEL_PGOUSER] = task.ObjectMeta.Labels[config.LABEL_PGOUSER]
	newjob.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER] = task.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER]

	backupType := task.Spec.Parameters[config.LABEL_PGHA_BACKUP_TYPE]
	if backupType != "" {
		newjob.ObjectMeta.Labels[config.LABEL_PGHA_BACKUP_TYPE] = backupType
	}
	kubeapi.CreateJob(clientset, &newjob, namespace)

	//publish backrest backup event
	if cmd == "backup" {
		topics := make([]string, 1)
		topics[0] = events.EventTopicBackup

		f := events.EventCreateBackupFormat{
			EventHeader: events.EventHeader{
				Namespace: namespace,
				Username:  task.ObjectMeta.Labels[config.LABEL_PGOUSER],
				Topic:     topics,
				Timestamp: time.Now(),
				EventType: events.EventCreateBackup,
			},
			Clustername: jobFields.ClusterName,
			BackupType:  "pgbackrest",
		}

		err := events.Publish(f)
		if err != nil {
			log.Error(err.Error())
		}
	}

}

// CreateInitialBackup creates a Pgtask in order to initiate the initial pgBackRest backup for a cluster
// as needed to support replica creation
func CreateInitialBackup(restclient *rest.RESTClient, namespace, clusterName, podName string) (*crv1.Pgtask, error) {
	var params map[string]string
	params = make(map[string]string)
	params[config.LABEL_PGHA_BACKUP_TYPE] = crv1.BackupTypeBootstrap
	return CreateBackup(restclient, namespace, clusterName, podName, params, "--type=full")
}

// CreatePostFailoverBackup creates a Pgtask in order to initiate the a pgBackRest backup following a failure
// event to ensure proper replica creation and/or reinitialization
func CreatePostFailoverBackup(restclient *rest.RESTClient, namespace, clusterName, podName string) (*crv1.Pgtask, error) {
	var params map[string]string
	params = make(map[string]string)
	params[config.LABEL_PGHA_BACKUP_TYPE] = crv1.BackupTypeFailover
	return CreateBackup(restclient, namespace, clusterName, podName, params, "")
}

// CreateBackup creates a Pgtask in order to initiate a pgBackRest backup
func CreateBackup(restclient *rest.RESTClient, namespace, clusterName, podName string, params map[string]string,
	backupOpts string) (*crv1.Pgtask, error) {

	log.Debug("pgBackRest operator CreateBackup called")

	cluster := crv1.Pgcluster{}
	_, err := kubeapi.Getpgcluster(restclient, &cluster, clusterName, namespace)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	var newInstance *crv1.Pgtask
	taskName := "backrest-backup-" + cluster.Name

	spec := crv1.PgtaskSpec{}
	spec.Name = taskName
	spec.Namespace = namespace

	spec.TaskType = crv1.PgtaskBackrest
	spec.Parameters = make(map[string]string)
	spec.Parameters[config.LABEL_JOB_NAME] = "backrest-" + crv1.PgtaskBackrestBackup + "-" + cluster.Name
	spec.Parameters[config.LABEL_PG_CLUSTER] = cluster.Name
	spec.Parameters[config.LABEL_POD_NAME] = podName
	spec.Parameters[config.LABEL_CONTAINER_NAME] = "database"
	spec.Parameters[config.LABEL_BACKREST_COMMAND] = crv1.PgtaskBackrestBackup
	spec.Parameters[config.LABEL_BACKREST_OPTS] = backupOpts
	spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE] = cluster.Spec.UserLabels[config.LABEL_BACKREST_STORAGE_TYPE]

	for k, v := range params {
		spec.Parameters[k] = v
	}

	newInstance = &crv1.Pgtask{
		ObjectMeta: meta_v1.ObjectMeta{
			Name: taskName,
		},
		Spec: spec,
	}
	newInstance.ObjectMeta.Labels = make(map[string]string)
	newInstance.ObjectMeta.Labels[config.LABEL_PG_CLUSTER] = cluster.Name
	newInstance.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER] = cluster.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER]
	newInstance.ObjectMeta.Labels[config.LABEL_PGOUSER] = cluster.ObjectMeta.Labels[config.LABEL_PGOUSER]

	err = kubeapi.Createpgtask(restclient, newInstance, cluster.Namespace)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return newInstance, nil
}

// CleanBackupResources is responsible for cleaning up Kubernetes resources from a previous
// pgBackRest backup.  Specifically, this function deletes the pgptask and job associate with a
// previous pgBackRest backup for the cluster.
func CleanBackupResources(restclient *rest.RESTClient, clientset *kubernetes.Clientset, namespace,
	clusterName string) error {

	pgtask := crv1.Pgtask{}
	taskName := "backrest-backup-" + clusterName
	// error if it already exists
	found, err := kubeapi.Getpgtask(restclient, &pgtask, taskName, namespace)
	if !found {
		log.Debugf("backrest backup pgtask %s was not found so we will create it", taskName)
	} else if err != nil {
		log.Error(err)
		return err
	}

	log.Debugf("pgtask %s was found so we will recreate it", taskName)
	//remove the existing pgtask
	err = kubeapi.Deletepgtask(restclient, taskName, namespace)
	if err != nil {
		return err
	}

	//remove previous backup job
	selector := config.LABEL_BACKREST_COMMAND + "=" + crv1.PgtaskBackrestBackup + "," +
		config.LABEL_PG_CLUSTER + "=" + clusterName + "," + config.LABEL_BACKREST + "=true"
	err = kubeapi.DeleteJobs(clientset, selector, namespace)
	if err != nil {
		log.Error(err)
	}

	timeout := time.After(30 * time.Second)
	tick := time.Tick(1 * time.Second)
	for {
		select {
		case <-timeout:
			return fmt.Errorf("Timed out waiting for deletion of pgBackRest backup job for "+
				"cluster %s", clusterName)
		case <-tick:
			jobList, err := kubeapi.GetJobs(clientset, selector, namespace)
			if err != nil {
				log.Error(err)
				return err
			}
			if len(jobList.Items) == 0 {
				return nil
			}
		}
	}
}
<commit_msg>This change configures all backrest jobs utilizing the pgo-backrest container (i.e. stanza-create and backup jobs) to set the IP address of the current primary as the value for the '--db-host' CLI option when running pgBackRest commands on the primary. Being that pgBackRest CLI options override environment variables, this will override the PG host specified via environment variable in the dedicated repository host, and will result in the dedicated repository host communicating directly with the primary pod when running pgBackRest commands, instead of going through the service configured for the pod.  Specifically, by communicating directly with the pod instead of through the service, this will ensure service issues do not impact communications between the dedicated repository host and primary when running backup and stanza create jobs.  The default PG host will remain set to the primary service (and not IP) to support the manual execution of pgBackRest commands through the service without the need to obtain the primary IP or reconfigure the repo when the primary changes.<commit_after>package backrest

/*
 Copyright 2019 Crunchy Data Solutions, Inc.
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
	"github.com/crunchydata/postgres-operator/config"
	"github.com/crunchydata/postgres-operator/events"
	"github.com/crunchydata/postgres-operator/kubeapi"
	"github.com/crunchydata/postgres-operator/operator"
	log "github.com/sirupsen/logrus"
	v1batch "k8s.io/api/batch/v1"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

type backrestJobTemplateFields struct {
	JobName                       string
	Name                          string
	ClusterName                   string
	Command                       string
	CommandOpts                   string
	PITRTarget                    string
	PodName                       string
	PGOImagePrefix                string
	PGOImageTag                   string
	SecurityContext               string
	PgbackrestStanza              string
	PgbackrestDBPath              string
	PgbackrestRepoPath            string
	PgbackrestRepoType            string
	BackrestLocalAndS3Storage     bool
	PgbackrestRestoreVolumes      string
	PgbackrestRestoreVolumeMounts string
}

// Backrest ...
func Backrest(namespace string, clientset *kubernetes.Clientset, task *crv1.Pgtask) {

	//create the Job to run the backrest command

	cmd := task.Spec.Parameters[config.LABEL_BACKREST_COMMAND]

	jobFields := backrestJobTemplateFields{
		JobName:                       task.Spec.Parameters[config.LABEL_JOB_NAME],
		ClusterName:                   task.Spec.Parameters[config.LABEL_PG_CLUSTER],
		PodName:                       task.Spec.Parameters[config.LABEL_POD_NAME],
		SecurityContext:               "",
		Command:                       cmd,
		CommandOpts:                   task.Spec.Parameters[config.LABEL_BACKREST_OPTS],
		PITRTarget:                    "",
		PGOImagePrefix:                operator.Pgo.Pgo.PGOImagePrefix,
		PGOImageTag:                   operator.Pgo.Pgo.PGOImageTag,
		PgbackrestStanza:              task.Spec.Parameters[config.LABEL_PGBACKREST_STANZA],
		PgbackrestDBPath:              task.Spec.Parameters[config.LABEL_PGBACKREST_DB_PATH],
		PgbackrestRepoPath:            task.Spec.Parameters[config.LABEL_PGBACKREST_REPO_PATH],
		PgbackrestRestoreVolumes:      "",
		PgbackrestRestoreVolumeMounts: "",
		PgbackrestRepoType:            operator.GetRepoType(task.Spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE]),
		BackrestLocalAndS3Storage:     operator.IsLocalAndS3Storage(task.Spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE]),
	}

	// if not already specified in the command options provided in the pgtask, then lookup the primary
	// pod for the cluster and add the hostname of the pod as the value for the "--db-host" parameter.
	// This will override the pg host configuration specified in the backrest repo, ensuring direct
	// communication between the repo pod and the primary via the primary's IP, instead of going through
	// the primary pod's service (which could be unreliable).
	if !strings.Contains(task.Spec.Parameters[config.LABEL_BACKREST_OPTS], "--db-host") &&
		!strings.Contains(task.Spec.Parameters[config.LABEL_BACKREST_OPTS], "--pg1-host") {
		selector := fmt.Sprintf("%s=%s,%s in (%s,%s)", config.LABEL_PG_CLUSTER,
			task.Spec.Parameters[config.LABEL_PG_CLUSTER], config.LABEL_PGHA_ROLE,
			"promoted", "master")
		pods, err := kubeapi.GetPods(clientset, selector, namespace)
		if err != nil {
			log.Error(err)
			return
		} else if len(pods.Items) > 1 {
			log.Errorf("More than one primary found when creating backrest job %s",
				task.Spec.Parameters[config.LABEL_JOB_NAME])
			return
		} else if len(pods.Items) == 0 {
			log.Errorf("Unable to find primary when creating backrest job %s",
				task.Spec.Parameters[config.LABEL_JOB_NAME])
			return
		}
		pod := pods.Items[0]
		jobFields.CommandOpts = jobFields.CommandOpts +
			fmt.Sprintf(" --db-host=%s", pod.Status.PodIP)
	}

	var doc2 bytes.Buffer
	err := config.BackrestjobTemplate.Execute(&doc2, jobFields)
	if err != nil {
		log.Error(err.Error())
		return
	}

	if operator.CRUNCHY_DEBUG {
		config.BackrestjobTemplate.Execute(os.Stdout, jobFields)
	}

	newjob := v1batch.Job{}
	err = json.Unmarshal(doc2.Bytes(), &newjob)
	if err != nil {
		log.Error("error unmarshalling json into Job " + err.Error())
		return
	}

	newjob.ObjectMeta.Labels[config.LABEL_PGOUSER] = task.ObjectMeta.Labels[config.LABEL_PGOUSER]
	newjob.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER] = task.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER]

	backupType := task.Spec.Parameters[config.LABEL_PGHA_BACKUP_TYPE]
	if backupType != "" {
		newjob.ObjectMeta.Labels[config.LABEL_PGHA_BACKUP_TYPE] = backupType
	}
	kubeapi.CreateJob(clientset, &newjob, namespace)

	//publish backrest backup event
	if cmd == "backup" {
		topics := make([]string, 1)
		topics[0] = events.EventTopicBackup

		f := events.EventCreateBackupFormat{
			EventHeader: events.EventHeader{
				Namespace: namespace,
				Username:  task.ObjectMeta.Labels[config.LABEL_PGOUSER],
				Topic:     topics,
				Timestamp: time.Now(),
				EventType: events.EventCreateBackup,
			},
			Clustername: jobFields.ClusterName,
			BackupType:  "pgbackrest",
		}

		err := events.Publish(f)
		if err != nil {
			log.Error(err.Error())
		}
	}

}

// CreateInitialBackup creates a Pgtask in order to initiate the initial pgBackRest backup for a cluster
// as needed to support replica creation
func CreateInitialBackup(restclient *rest.RESTClient, namespace, clusterName, podName string) (*crv1.Pgtask, error) {
	var params map[string]string
	params = make(map[string]string)
	params[config.LABEL_PGHA_BACKUP_TYPE] = crv1.BackupTypeBootstrap
	return CreateBackup(restclient, namespace, clusterName, podName, params, "--type=full")
}

// CreatePostFailoverBackup creates a Pgtask in order to initiate the a pgBackRest backup following a failure
// event to ensure proper replica creation and/or reinitialization
func CreatePostFailoverBackup(restclient *rest.RESTClient, namespace, clusterName, podName string) (*crv1.Pgtask, error) {
	var params map[string]string
	params = make(map[string]string)
	params[config.LABEL_PGHA_BACKUP_TYPE] = crv1.BackupTypeFailover
	return CreateBackup(restclient, namespace, clusterName, podName, params, "")
}

// CreateBackup creates a Pgtask in order to initiate a pgBackRest backup
func CreateBackup(restclient *rest.RESTClient, namespace, clusterName, podName string, params map[string]string,
	backupOpts string) (*crv1.Pgtask, error) {

	log.Debug("pgBackRest operator CreateBackup called")

	cluster := crv1.Pgcluster{}
	_, err := kubeapi.Getpgcluster(restclient, &cluster, clusterName, namespace)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	var newInstance *crv1.Pgtask
	taskName := "backrest-backup-" + cluster.Name

	spec := crv1.PgtaskSpec{}
	spec.Name = taskName
	spec.Namespace = namespace

	spec.TaskType = crv1.PgtaskBackrest
	spec.Parameters = make(map[string]string)
	spec.Parameters[config.LABEL_JOB_NAME] = "backrest-" + crv1.PgtaskBackrestBackup + "-" + cluster.Name
	spec.Parameters[config.LABEL_PG_CLUSTER] = cluster.Name
	spec.Parameters[config.LABEL_POD_NAME] = podName
	spec.Parameters[config.LABEL_CONTAINER_NAME] = "database"
	spec.Parameters[config.LABEL_BACKREST_COMMAND] = crv1.PgtaskBackrestBackup
	spec.Parameters[config.LABEL_BACKREST_OPTS] = backupOpts
	spec.Parameters[config.LABEL_BACKREST_STORAGE_TYPE] = cluster.Spec.UserLabels[config.LABEL_BACKREST_STORAGE_TYPE]

	for k, v := range params {
		spec.Parameters[k] = v
	}

	newInstance = &crv1.Pgtask{
		ObjectMeta: meta_v1.ObjectMeta{
			Name: taskName,
		},
		Spec: spec,
	}
	newInstance.ObjectMeta.Labels = make(map[string]string)
	newInstance.ObjectMeta.Labels[config.LABEL_PG_CLUSTER] = cluster.Name
	newInstance.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER] = cluster.ObjectMeta.Labels[config.LABEL_PG_CLUSTER_IDENTIFIER]
	newInstance.ObjectMeta.Labels[config.LABEL_PGOUSER] = cluster.ObjectMeta.Labels[config.LABEL_PGOUSER]

	err = kubeapi.Createpgtask(restclient, newInstance, cluster.Namespace)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return newInstance, nil
}

// CleanBackupResources is responsible for cleaning up Kubernetes resources from a previous
// pgBackRest backup.  Specifically, this function deletes the pgptask and job associate with a
// previous pgBackRest backup for the cluster.
func CleanBackupResources(restclient *rest.RESTClient, clientset *kubernetes.Clientset, namespace,
	clusterName string) error {

	pgtask := crv1.Pgtask{}
	taskName := "backrest-backup-" + clusterName
	// error if it already exists
	found, err := kubeapi.Getpgtask(restclient, &pgtask, taskName, namespace)
	if !found {
		log.Debugf("backrest backup pgtask %s was not found so we will create it", taskName)
	} else if err != nil {
		log.Error(err)
		return err
	}

	log.Debugf("pgtask %s was found so we will recreate it", taskName)
	//remove the existing pgtask
	err = kubeapi.Deletepgtask(restclient, taskName, namespace)
	if err != nil {
		return err
	}

	//remove previous backup job
	selector := config.LABEL_BACKREST_COMMAND + "=" + crv1.PgtaskBackrestBackup + "," +
		config.LABEL_PG_CLUSTER + "=" + clusterName + "," + config.LABEL_BACKREST + "=true"
	err = kubeapi.DeleteJobs(clientset, selector, namespace)
	if err != nil {
		log.Error(err)
	}

	timeout := time.After(30 * time.Second)
	tick := time.Tick(1 * time.Second)
	for {
		select {
		case <-timeout:
			return fmt.Errorf("Timed out waiting for deletion of pgBackRest backup job for "+
				"cluster %s", clusterName)
		case <-tick:
			jobList, err := kubeapi.GetJobs(clientset, selector, namespace)
			if err != nil {
				log.Error(err)
				return err
			}
			if len(jobList.Items) == 0 {
				return nil
			}
		}
	}
}
<|endoftext|>